0
点赞
收藏
分享

微信扫一扫

Linux--FTP服务器功能--项目

双井暮色 2023-10-08 阅读 7

线性表

单链表

**单链表操作**

#include<iostream>
using namespace std;

typedef struct LNode{
	int data;
	struct LNode *next;
}LNode,*LinkList;

//初始化 
void InitList(LinkList &L){
	L=(LNode *)malloc(sizeof(LinkList));
	L->next=NULL;
}

//头插法建表
LinkList HeadInsert(LinkList &L){
	InitList(L);
	int x;
	while(cin>>x){
		LNode *s=(LNode *)malloc(sizeof(LNode));
		s->data=x;
		s->next=L->next;
		L->next=s;
	}
	return L;
}

//尾插法建表
LinkList TailInsert(LinkList &L){
	InitList(L);
	LNode *s,*r=L;
	int x;
	while(cin>>x){
		s=(LNode *)malloc(sizeof(LNode));
		s->data=x;
		r->next=s;
		r=s;
	}
	r->next=NULL;
	return L;
} 

//输出表中元素
void PrintList(LinkList L){
	LNode *p=L->next;
	while(p!=NULL){
		cout<<p->data<<" ";
		p=p->next;
	}
} 

//求单链表的长度
int Length(LinkList L){
    LNode *p = L->next;
    int len = 0;
    while(p!=NULL){
        len++;
        p = p->next;
    }
    return len;
}

//按值查找:查找x在L中的位置
LNode *LocateElem(LinkList L, int x){
    LNode *p = L->next;
    while(p!=NULL&&p->data!=x){
        p = p->next;
    }
    return p;
}

//按位查找:查找在单链表L中第i个位置的结点
LNode *GetElem(LinkList L, int i){
    int j=1;
    LNode *p = L->next;
    if(i==0){
		return L;
	}
    if(i<1){
		return NULL;
	}
    while(p!=NULL&&j<i){
        p = p->next;
        j++;
    }
    return p;
}

//判空操作
void Empty(LinkList L){
    if(L->next == NULL){
        cout<<"L is null"<<endl;
    }else{
        cout<<"L is not null"<<endl;
    }
}

//将x插入到单链表L的第i个位置上
void Insert(LinkList &L, int i, int x){
    LNode *p = GetElem(L,i-1);
    LNode *s = (LNode *)malloc(sizeof(LNode));
    s->data = x;
    s->next = p->next;
    p->next = s;
}

//删除操作:将单链表中的第i个结点删除
void Delete(LinkList &L, int i){
	if(i<0||i>Length(L)){
		cout<<"数据有误"; 
		return ;
	}
    LNode *p = GetElem(L,i-1);
    LNode *q = p->next;
    p->next = q->next;
    free(q);
}

int main(){
	**自定义补充**
	return 0;
}

双链表

循环链表

栈、队列、数组

查找

排序

插入排序

直接插入排序

#include<iostream>
using namespace std;

void InsertSort(int a[],int len){
	int temp,i,j;
	for(i=1;i<len;i++){
		if(a[i]<a[i-1]){
			temp=a[i];
			for(j=i-1;temp<a[j];j--){
				a[j+1]=a[j];	
			}
			a[j+1]=temp;
		}
	}
} 

int main(){
	int a[]={1,3,4,2,6,5,9,8,7};
	int l=sizeof(a)/sizeof(int);
	InsertSort(a,l);
	for(int i=0;i<l;i++){
		cout<<a[i]<<" ";
	} 
	return 0;
}

折半插入排序

#include<iostream>
using namespace std;

void InsertSort(int a[],int len){
	int temp,low,high,mid;
	for(int i=1;i<len;i++){
		temp=a[i];
		low=0,high=i-1;
		while(low<=high){
			mid=(low+high)/2;
			if(a[mid]>temp){
				high=mid-1;
			}else{
				low=mid+1;
			}
		}
		for(int j=i-1;j>=high+1;j--){
			a[j+1]=a[j];
		}
		a[high+1]=temp;
	}
}

int main(){
	int a[]={1,3,4,2,6,5,9,8,7};
	int l=sizeof(a)/sizeof(int);
	InsertSort(a,l);
	for(int i=0;i<l;i++){
		cout<<a[i]<<" ";
	}
	return 0;
}

希尔排序

#include<iostream>
using namespace std;

void InsertSort(int a[],int len){
	for(int d=len/2;d>=1;d=d/2){
		int temp,i,j;
		for(i=d+1;i<len;i++){
			if(a[i]<a[i-d]){
				temp=a[i];
				for(j=i-d;j>0&&temp<a[j];j-=d){
					a[j+d]=a[j];	
				}
				a[j+d]=temp;
			}
		}
	}
} 

int main(){
	int a[]={1,3,4,2,6,5,9,8,7};
	int l=sizeof(a)/sizeof(int);		
	InsertSort(a,l);
	for(int i=0;i<l;i++){
		cout<<a[i]<<" ";
	}
	return 0;
}

交换排序

冒泡排序

#include<iostream>
using namespace std;

void BubbleSort(int a[],int len){
	for(int i=0;i<len-1;i++){
		for(int j=0;j<len-i;j++){
			if(a[j+1]<a[j]){
				swap(a[j+1],a[j]);
			}
		}
	}
} 

int main(){
	int a[]={1,3,4,2,6,5,9,8,7};
	int l=sizeof(a)/sizeof(int);		
	BubbleSort(a,l);
	for(int i=0;i<l;i++){
		cout<<a[i]<<" ";
	}
	return 0;
}
void BubbleSort(int a[],int len){
	for(int i=0;i<len-1;i++){
		bool flag=false; 
		for(int j=0;j<len-i;j++){
			if(a[j+1]<a[j]){
				swap(a[j+1],a[j]);
				flag=true;
			}
		}
		if(flag==false){
			return ;
		}
	}
} 

快速排序

#include<iostream>
using namespace std;

void QuickSort(int a[],int low,int high){
	int i=low,j=high,pivot=a[low];
	if(low>=high){
		return ;
	}
	while(i<j){
		while(a[j]>=pivot&&i<j){
			j--;
		}
		a[i]=a[j];
		while(a[i]<=pivot&&i<j){
			i++;
		}
		a[j]=a[i];
	}
	a[i]=pivot;
	QuickSort(a,low,i-1);
	QuickSort(a,i+1,high);
}

int main(){
	int a[]={1,3,4,2,6,5,9,8,7};
	int l=sizeof(a)/sizeof(int);		
	QuickSort(a,0,l-1);
	for(int i=0;i<l;i++){
		cout<<a[i]<<" ";
	}
	return 0;
}
**快速排序另一种写法:**

int Partition(int a[],int low,int high){
	int pivot=a[low];
	while(low<high){
		while(a[high]>pivot&&low<high){
			high--;
		}
		a[low]=a[high];
		while(a[low]<pivot&&low<high){
			low++;
		}
		a[high]=a[low];
	}
	a[low]=pivot;
	return low;
}

void QuickSort(int a[],int low,int high){
	if(low<high){
		int pivotpos = Partition(a,low,high);
		QuickSort(a,low,pivotpos-1);
		QuickSort(a,pivotpos+1,high);
	}
}

选择排序

简单选择排序

#include<iostream>
using namespace std;

void SelectSort(int a[],int len){
	for(int i=0;i<len-1;i++){
		int min=i;
		for(int j=i+1;j<len;j++){
			if(a[j]<a[min]){
				min=j;
			}
		}
		if(min!=i){
			swap(a[i],a[min]);
		}
	}
} 

int main(){
	int a[]={1,3,4,2,6,5,9,8,7};
	int l=sizeof(a)/sizeof(int);		
	SelectSort(a,l);
	for(int i=0;i<l;i++){
		cout<<a[i]<<" ";
	}
	return 0;
}

堆排序

#include<iostream>
using namespace std;

void HeapAdjust(int a[],int k,int len){
	a[0]=a[k];
	for(int i=2*k;i<=len;i*=2){
		if(i<len&&a[i]<a[i+1]){
			i++;	
		}
		if(a[0]>=a[i]){
			break;
		}else{
			a[k]=a[i];
			k=i;
		}
	}
	a[k]=a[0];
}

void BuildMaxHeap(int a[],int len){
	for(int i=len/2;i>0;i--){
		HeapAdjust(a,i,len);
	}
}

void HeapSort(int a[],int len){
	BuildMaxHeap(a,len);
	for(int i=len;i>1;i--){
		swap(a[i],a[1]);
		HeapAdjust(a,1,i-1);
	}
} 

int main(){
	int b[]={1,3,4,2,6,5,9,8,7};
	int a[100],l=sizeof(b)/sizeof(int);
	for(int i=0;i<l;i++){
		a[i+1]=b[i];
	}
	HeapSort(a,l);
	for(int i=1;i<=l;i++){
		cout<<a[i]<<" ";
	}
	return 0;
} 

归并排序

在这里插入图片描述

int b[high-low+1];  
int i = low, j = mid + 1, k = 0;    
while (i <= mid && j <= high) {
	if (a[i] <= a[j]) {
		b[k++] = a[i++]; 
	} else {
		b[k++] = a[j++];
	}
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

while (i <= mid){                         // j 序列结束,将剩余的 i 序列补充在 b 数组中 
	b[k++] = a[i++];
}
while (j <= high){                        // i 序列结束,将剩余的 j 序列补充在 b 数组中 
	b[k++] = a[j++];
}
while (i <= mid && j <= high) {
	if (a[i] <= a[j]) {
		b[k++] = a[i++]; 
	} else {
		b[k++] = a[j++];
	}
}
for (int i = low; i <= hight; i++)  {          //将 b 数组的值传递给数组 a
	a[i] = b[k++];
}
delete[]b;               // 辅助数组用完后,将其的空间进行释放(销毁)
void MergeSort(int a[ ],int low,int high){
	if(low<high){
		int mid=(low+high)/2;
		MergeSort(a,low,mid);
		MergeSort(a,mid+1,high);
		Merge(a,low,mid,high);
	}
}
**完整代码**
#include<iostream>
using namespace std;

void Merge(int a[],int low,int mid,int high){
	int *b=new int[high-low+1];               //用 new 申请一个辅助函数
	int i = low, j = mid + 1, k = 0;          // k为 b 数组的小标
	while (i <= mid && j <= high){
		if (a[i] <= a[j]){
			b[k++] = a[i++];                 //按从小到大存放在 b 数组里面
		}else{
			b[k++] = a[j++];
		}
	}
	while (i <= mid){                        // j 序列结束,将剩余的 i 序列补充在 b 数组中 
		b[k++] = a[i++];
	}
	while (j <= high){                       // i 序列结束,将剩余的 j 序列补充在 b 数组中 
		b[k++] = a[j++];
	}
	k = 0;                                   //从小标为 0 开始传送
	for (int i = low; i <= high; i++){       //将 b 数组的值传递给数组 a
		a[i] = b[k++];
	}
	delete []b;                              //销毁 
}

void MergeSort(int a[],int low,int high){
	if(low<high){
		int mid=(low+high)/2;
		MergeSort(a,low,mid);
		MergeSort(a,mid+1,high);
		Merge(a,low,mid,high);
	}
}

int main(){
	int a[]={1,3,4,2,6,5,9,8,7};
	int l=sizeof(a)/sizeof(int);
	MergeSort(a,0,l-1);
	for(int i=0;i<l;i++){
		cout<<a[i]<<" ";
	}
	return 0;
}

基数排序

举报

相关推荐

0 条评论