C语言实现动态顺序表的实现代码
C语言实现动态顺序表的实现代码
顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。线性表采用顺序存储的方式存储就称之为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。
静态实现:结构体内部只需两个成员,其中一个为固定大小(MAX)的数组,用来存放我们的数据。数组大小我们可以通过在头文件中改变MAX的值来改变。
动态实现:在内存中开辟一块空间,可以随我们数据数量的增多来扩容。
来看看动态的顺序表实现:
1.seqlist.h
#define_CRT_SECURE_NO_WARNINGS1 #ifndef__SEQLIST_H__ #define__SEQLIST_H__ #include#include #include #include typedefintDataType; #defineDEFAULT_SZ3 #defineINC_SZ2 typedefstructSeqList { DataType*data; intsz; intcapacity; }SeqList,*pSeqList; voidInitSeqList(pSeqListpList); voidPushBack(pSeqListpList,DataTyped); voidPopBack(pSeqListpList); voidPushFront(pSeqListpList,DataTyped); voidPopFront(pSeqListpList); intFind(pSeqListpList,DataTyped); voidRemove(pSeqListpList,DataTyped); voidRemoveAll(pSeqListpList,DataTyped); voidBubbleSort(pSeqListpList); intBinarySearch(pSeqListpList,DataTyped); voidPrintfSeqList(pSeqListpList); voidInsert(pSeqListpList,intpos,DataTyped); voidReverseList(pSeqListpList); voidDestroySeqList(pSeqListpList); #endif//__SEQLIST_H__
2.seqlist.c
#define_CRT_SECURE_NO_WARNINGS1 #include"seqlist.h" voidInitSeqList(pSeqListpList) { pList->sz=0; pList->data=(DataType*)malloc(sizeof(DataType)*DEFAULT_SZ); if(pList->data==NULL) { perror("malloc"); return; } memset(pList->data,0,sizeof(DataType)*DEFAULT_SZ); } voidCheckCapacity(pSeqListpList) { assert(pList); if(pList->sz==pList->capacity) { DataType*ret=(DataType*)realloc(pList->data,sizeof(DataType)*(pList->capacity+INC_SZ)); if(ret==NULL) { perror("realloc"); } pList->data=ret; pList->capacity+=INC_SZ; } } voidPushBack(pSeqListpList,DataTyped) { assert(pList); if(pList->sz==pList->capacity) { CheckCapacity(pList); } pList->data[pList->sz]=d; pList->sz++; } voidPopBack(pSeqListpList) { inti=0; assert(pList); if(pList->sz==0) { printf("顺序表为空:<"); return; } pList->sz--; } voidPushFront(pSeqListpList,DataTyped) { inti=0; assert(pList); if(pList->sz==pList->capacity) { CheckCapacity(pList); } for(i=pList->sz;i>=1;i--) { pList->data[i]=pList->data[i-1]; } pList->data[0]=d; pList->sz++; } voidPopFront(pSeqListpList) { inti=0; assert(pList); for(i=0;isz;i++) { pList->data[i]=pList->data[i+1]; } pList->sz--; } intFind(pSeqListpList,DataTyped) { inti=0; assert(pList); while(i sz) { if(pList->data[i]==d) { returni; } else { i++; } } return-1; } voidRemove(pSeqListpList,DataTyped) { inti=0; intpos=0; assert(pList); while(pList->data[pos=Find(pList,d)]==d) { for(i=pos;i sz-1;i++) { pList->data[i]=pList->data[i+1]; } pList->sz--; } } voidRemoveAll(pSeqListpList,DataTyped) { inti=0; intpos=0; assert(pList); while((pos=Find(pList,d))!=-1) { for(i=pos;i sz-1;i++) { pList->data[i]=pList->data[i+1]; } pList->sz--; } } voidBubbleSort(pSeqListpList) { inti=0; assert(pList); for(i=0;i sz-1;i++) { intj=0; for(j=0;j sz-i-1;j++) { if(pList->data[j]>pList->data[j+1]) { DataTypetmp=pList->data[j]; pList->data[j]=pList->data[j+1]; pList->data[j+1]=tmp; } } } } intBinarySearch(pSeqListpList,DataTyped) { intleft=0; intright=pList->sz-1; assert(pList); while(left<=right) { intmid=left-((left-right)>>1); if(d>pList->data[mid]) { left=mid+1; } elseif(d data[mid]) { right=mid-1; } else returnmid; } return-1; } voidPrintfSeqList(pSeqListpList) { inti=0; for(i=0;i sz;i++) { printf("%d",pList->data[i]); } } voidInsert(pSeqListpList,intpos,DataTyped) { inti=0; if(pList->sz==pList->capacity) { CheckCapacity(pList); } for(i=pList->sz-1;i>=pos;i--) { pList->data[i+1]=pList->data[i]; } pList->data[pos]=d; pList->sz++; } voidReverseList(pSeqListpList) { intleft=0; intright=pList->sz-1; assert(pList); while(left data[left]; pList->data[left]=pList->data[right]; pList->data[right]=tmp; left++; right--; } } voidDestroySeqList(pSeqListpList) { free(pList->data); pList->data=NULL; }
3.test.c
#define_CRT_SECURE_NO_WARNINGS1 #include"seqlist.h" //voidTest() //{ //SeqList*List; //InitSeqList(&List); //PushBack(&List,1); //PushBack(&List,2); //PushBack(&List,3); //PushBack(&List,4); //PushBack(&List,5); //PopBack(&List); //printf("%d",Find(&List,2)); //PrintfSeqList(&List); //} voidTest2() { SeqListList; InitSeqList(&List); PushBack(&List,1); PushBack(&List,2); PushBack(&List,3); PushBack(&List,4); PushBack(&List,5); PushFront(&List,5); PushFront(&List,2); PushFront(&List,3); //PopFront(&List); RemoveAll(&List,5); //BubbleSort(&List); //BinarySearch(&List,3); PrintfSeqList(&List); } intmain() { Test2(); system("pause\n"); return0; }
静态顺序表的实现:https://www.nhooo.com/article/120875.htm
以上就是动态实现顺序表的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!