顺序表的增删查改、二分查找、冒泡和快速排序
SeqList声明文件#pragmaonce#defineMAX_SIZE5typedefintDataType;typedefstructSeqList{DataTypearray[MAX_SIZE];size_tsize;}SeqList;voidPrintSeqList(SeqList*pSeq);voidInitSeqList(SeqList*pSeq);//初始化voidPushBack(SeqList*pSeq,DataTypex);//尾插voidPopBack(SeqList*pSeq);//尾删voidPushFront(SeqList*pSeq,DataTypex);//头插voidPopFront(SeqList*pSeq);voidInsert(SeqList*pSeq,size_tpos,DataTypex);//插入intFind(SeqList*pSeq,size_tpos,DataTypex);//查找voidErase(SeqList*pSeq,size_tpos);//删除intRemove(SeqList*pSeq,DataTypex);voidRemoveAll(SeqList*pSeq,DataTypex);//去重复voidSwap(DataType*left,DataType*right);voidBubbleSort(SeqList*pSeq);//冒泡voidSelectSort(SeqList*pSeq);//voidSelectSort_OP(SeqList*pSeq)//选择intBinarySearch(SeqList*pSeq,DataTypex);//二分查找SeqList实现文件#include"SeqList.h"#include<assert.h>#include<iostream>#include<string.h>usingnamespacestd;voidInitSeqList(SeqList*pSeq){memset(pSeq->array,0,sizeof(DataType)*MAX_SIZE);pSeq->size=0;}voidPushBack(SeqList*pSeq,DataTypex){assert(pSeq);if(pSeq->size>=MAX_SIZE){printf("SeqListisfull\n");return;}pSeq->array[pSeq->size]=x;pSeq->size++;}voidPopBack(SeqList*pSeq){assert(pSeq);if(pSeq->size<=0){printf("SeqListisEmpty\n");return;}pSeq->size--;}voidPushFront(SeqList*pSeq,DataTypex){intbegin=pSeq->size-1;assert(pSeq);if(pSeq->size>=MAX_SIZE){printf("SeqListisfull\n");return;}for(begin;begin>=0;begin--){pSeq->array[begin+1]=pSeq->array[begin];}pSeq->array[0]=x;pSeq->size++;}voidPopFront(SeqList*pSeq){intbegin=0;assert(pSeq);if(pSeq->size<=0){printf("SeqListisEmpty\n");}for(begin;begin<pSeq->size;begin++){pSeq->array[begin]=pSeq->array[begin+1];}pSeq->size--;}voidInsert(SeqList*pSeq,size_tpos,DataTypex)//插入位置按数组下标顺序{intbegin=pSeq->size;assert(pSeq);assert(pos<=pSeq->size);if(pSeq->size>=MAX_SIZE){printf("SeqListisfull\n");return;}for(begin;begin>pos;begin--){pSeq->array[begin]=pSeq->array[begin-1];}pSeq->array[pos]=x;pSeq->size++;}intFind(SeqList*pSeq,size_tpos,DataTypex)//查找返回数组下标位置{inti=pos;assert(pSeq);assert(pos<=pSeq->size);if(pSeq->size<=0){printf("SeqListisEmpty\n");}for(i;i<pSeq->size;i++){if(pSeq->array[i]==x){returni;}}return-1;}voidErase(SeqList*pSeq,size_tpos){assert(pSeq);assert(pos<=pSeq->size);if(pSeq->size<=0){printf("SeqListisEmpty\n");return;}for(pos;pos<pSeq->size;pos++){pSeq->array[pos]=pSeq->array[pos+1];}pSeq->size--;}voidPrintSeqList(SeqList*pSeq){inti=0;assert(pSeq);for(;i<pSeq->size;i++){printf("%d",pSeq->array[i]);}cout<<endl;}intRemove(SeqList*pSeq,DataTypex)//删除{intpos;assert(pSeq);pos=Find(pSeq,0,x);if(pos!=-1){Erase(pSeq,pos);}returnpos;}voidRemoveAll(SeqList*pSeq,DataTypex)//去掉重复{intcount=0;intbegin=0;assert(pSeq);for(;begin<pSeq->size;begin++){if(pSeq->array[begin]==x){count++;}else{pSeq->array[begin-count]=pSeq->array[begin];}}pSeq->size-=count;}voidSwap(DataType*left,DataType*right){DataTypetmp=*left;*left=*right;*right=tmp;}voidBubbleSort(SeqList*pSeq)//冒泡排序{assert(pSeq);intfirst=0;intsecond=0;for(first=0;first<pSeq->size-1;first++){//intFlag=0;for(second=1;second<pSeq->size-first;second++){if(pSeq->array[second-1]>pSeq->array[second]){Swap(&pSeq->array[second-1],&pSeq->array[second]);//Flag=1;}}/*if(Flag==0){return;}*/}}voidSelectSort(SeqList*pSeq)//快速排序{assert(pSeq);inti,j,max;for(j=pSeq->size-1;j>=0;j--){max=j;for(i=j-1;i>=0;i--){if(pSeq->array[max]<pSeq->array[i]){max=i;}}Swap(&pSeq->array[max],&pSeq->array[j]);}}voidSelectSort_OP(SeqList*pSeq)//快排优化{assert(pSeq);inti,min,max;intleft=0;intright=pSeq->size-1;while(left<right){for(i=left;i<=right;i++){min=left;max=right;if(pSeq->array[min]>pSeq->array[i]){Swap(&pSeq->array[left],&pSeq->array[i]);}if(pSeq->array[max]<pSeq->array[i]){Swap(&pSeq->array[max],&pSeq->array[i]);}}left++;right--;}}intBinarySearch(SeqList*pSeq,DataTypex)//二分查找{intleft=0;intright=pSeq->size-1;while(left<right){intmid=left+(right-left)/2;if(pSeq->array[mid]>x){right=mid-1;}elseif(pSeq->array[mid]<x){left=mid+1;}else{returnmid;}}return-1;}测试文件intmain(){SeqListseqlist;InitSeqList(&seqlist);/*PushBack(&seqlist,1);PushBack(&seqlist,2);PushBack(&seqlist,3);PushBack(&seqlist,4);PrintSeqList(&seqlist);PopBack(&seqlist);PopBack(&seqlist);PopBack(&seqlist);*/PushFront(&seqlist,2);PushFront(&seqlist,1);PushFront(&seqlist,4);PushFront(&seqlist,3);PushFront(&seqlist,5);PrintSeqList(&seqlist);intkey=BinarySearch(&seqlist,4);printf("%d",key);cout<<key<<endl;//BubbleSort(&seqlist);//SelectSort_OP(&seqlist);PrintSeqList(&seqlist);//RemoveAll(&seqlist,3);/*Erase(&seqlist,0);PrintSeqList(&seqlist);Erase(&seqlist,0);Erase(&seqlist,0);Erase(&seqlist,0);PrintSeqList(&seqlist*///intvalue=Remove(&seqlist,4);//printf("%d\n",value);//Insert(&seqlist,0,1);//PrintSeqList(&seqlist);/*PopFront(&seqlist);PopFront(&seqlist);PrintSeqList(&seqlist);PopFront(&seqlist);PopFront(&seqlist);PopFront(&seqlist);PopFront(&seqlist);*/system("pause");return0;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。