日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

动态顺序表

發布時間:2023/11/30 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 动态顺序表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//SeqList.h #ifndef?__SEQLIST_H__ #define?__SEQLIST_H__ #include<string.h> #include<assert.h> #include<stdlib.h>typedef?int?DataType; typedef?struct?SeqList {DataType*?array;size_t?size;DataType?capacity;????//容量 }SeqList,*pSeqList;void?InitSeqList(pSeqList?pSeq); void?PushBack(pSeqList?pSeq,DataType?x); void?PrintSeqList(pSeqList?pSeq);void?PopBack(pSeqList?pSeq); void?PushFront(pSeqList?pSeq,?DataType?x); void?PopFront(pSeqList?pSeq);void?Insert(pSeqList?pSeq,?int?pos,?DataType?x); void?Remove(pSeqList?pSeq,?DataType?x); void?RemoveAll(pSeqList?pSeq,?DataType?x);int?Find(pSeqList?pSeq,DataType?x); void?Erase(pSeqList?pSeq,DataType?pos); void?ReverseList(pSeqList?pSeq);void?SortList(pSeqList?pSeq); void?SelectSortList(pSeqList?pSeq); int??BinarySearch(pSeqList?Seq,?DataType?x);void?CheckCapacity(pSeqList?pSeq)???//檢查容量 {if(pSeq->size?>=?pSeq->capacity){pSeq->capacity?=?pSeq->capacity*2+3;pSeq->array?=?(DataType*)realloc(pSeq->array,pSeq->capacity*sizeof(DataType));// DataType?*tmp?=?(DataType?*)malloc(sizeof(DataType)*pSeq->capacity*2);// memcpy(tmp,pSeq->array,sizeof(DataType)*pSeq->size);// free(pSeq->array);// pSeq->array?=?tmp;} }void?InitSeqList(pSeqList?pSeq)??//初始化順序表 {assert(pSeq); // pSeq->array?=?(DataType*)malloc(sizeof(DataType)*3); // pSeq->size?=?0; // pSeq->capacity?=?3;pSeq->array?=?NULL;pSeq->size?=?0;pSeq->capacity?=?0; }void?PushBack(pSeqList?pSeq,DataType?x)??//尾插 {assert(pSeq);CheckCapacity(pSeq);pSeq->array[pSeq->size++]?=?x; }void?PopBack(pSeqList?pSeq)?????//尾刪 {assert(pSeq);if(pSeq->size?<=?0){printf("SeqList?is?empty");return;}pSeq->array[pSeq->size-1]?=?NULL;pSeq->size--; }void?PushFront(pSeqList?pSeq,?DataType?x)???//頭插 {int?i?=?0;assert(pSeq);CheckCapacity(pSeq);for(i?=?pSeq->size-1;?i?>=?0;?i--){pSeq->array[i+1]?=?pSeq->array[i];}pSeq->array[0]?=?x;pSeq->size++; } void?PopFront(pSeqList?pSeq)????//頭刪 {int?i?=?0;assert(pSeq);if(pSeq->size?<=?0){printf("SeqList?is?empty");return;}for(i?=?1;?i?<?pSeq->size;?i++){pSeq->array[i-1]?=?pSeq->array[i];}pSeq->size--; }void?Destroy(pSeqList?pSeq)??//銷毀 {assert(pSeq);pSeq->array?=?NULL;pSeq->size?=?0;pSeq->capacity?=?0; }void?PrintSeqList(pSeqList?pSeq)??//打印 {int?i?=?0;assert(pSeq);for(i?=?0;?i?<?pSeq->size;i++){printf("%d?",pSeq->array[i]);} }void?Insert(pSeqList?pSeq,?int?pos,?DataType?x)??//在某個位置處插入 {int?i?=?0;assert(pSeq);CheckCapacity(pSeq);for(i?=?pSeq->size-1;?i?>=?pos;?i--){pSeq->array[i+1]?=?pSeq->array[i];}pSeq->array[pos]?=?x;pSeq->size++; } int?Find(pSeqList?pSeq,DataType?x)????//查找 {int?i?=?0;assert(pSeq);if(pSeq->size?<=?0){printf("SeqList?is?empty");return?-1;}for(i?=?0;?i?<?pSeq->size;?i++){if(pSeq->array[i]?==?x){return?i;}}return?-1; }void?Erase(pSeqList?pSeq,DataType?pos)???//按位置刪除 {int?i?=?0;assert(pSeq);if(pSeq->size?<=?0){printf("SeqList?is?empty");return;}for(i?=?pos+1;?i?<?pSeq->size;?i++){pSeq->array[i-1]?=?pSeq->array[i];}pSeq->size--; } void?Remove(pSeqList?pSeq,?DataType?x)???//刪除 {int?pos?=?Find(pSeq,x);assert(pSeq);if(pos?!=?-1){Erase(pSeq,pos);}else{printf("not?exist");} } void?RemoveAll(pSeqList?pSeq,?DataType?x)??//刪除所有的x {int?count?=?0;int?i?=?0;assert(pSeq);if(pSeq->size?<=?0){printf("SeqList?is?empty");return;}for(i?=?0;?i?<?pSeq->size;?i++){if(pSeq->array[i]?==?x){count++;}else{pSeq->array[i-count]?=?pSeq->array[i];}}pSeq->size-=count; }void?ReverseList(pSeqList?pSeq)???//?逆置 {int?left?=?0;int?right?=?pSeq->size-1;assert(pSeq);while(left?<?right){int?tmp?=?pSeq->array[left];pSeq->array[left]?=?pSeq->array[right];pSeq->array[right]?=?tmp;left++;right--;} }void?SortList(pSeqList?pSeq)??//冒泡排序 {int?i?=?0,?j?=?0;assert(pSeq);for(i?=?0;?i?<?pSeq->size-1;?i++){int?exchange?=?0;for(j?=?0;?j?<?pSeq->size-i-1;?j++){if(pSeq->array[j]?>?pSeq->array[j+1]){int?tmp?=?pSeq->array[j];pSeq->array[j]?=?pSeq->array[j+1];pSeq->array[j+1]?=?tmp;exchange?=?1;}}if(exchange?==?0){return;}} }void?SelectSortList(pSeqList?pSeq)???//選擇排序 {int?i?=?0,?j?=?0;int?min?=?0;int?tmp;assert(pSeq);for(i?=?0;?i?<?pSeq->size-1;?i++){min?=?i;for(j?=?i+1;?j?<?pSeq->size;?j++){if(pSeq->array[min]?>?pSeq->array[j]){min?=?j;}tmp?=?pSeq->array[min];pSeq->array[min]?=?pSeq->array[i];pSeq->array[i]?=?tmp;}} }int?BinarySearch(pSeqList?pSeq,?DataType?x)???//二分查找 {int?left?=?0;int?right?=?pSeq->size-1;assert(pSeq);while(left?<=?right){int?mid?=?left?-?(left?-?right)/2;if(pSeq->array[mid]?>?x){right?=?mid-1;}else?if(pSeq->array[mid]?<?x){left?=?mid+1;}else{return?mid;}}return?-1; }#endif?//?__SEQLIST_H__//SeqList.c #include<stdio.h> #include"SeqList.h"void?Test1() {SeqList?seq;InitSeqList(&seq);PushBack(&seq,1);PushBack(&seq,2);PushBack(&seq,3);PushBack(&seq,4);PopBack(&seq);PrintSeqList(&seq);Destroy(&seq); } void?Test2() {SeqList?seq;int?ret;InitSeqList(&seq);PushBack(&seq,1);PushBack(&seq,2);PushBack(&seq,3);PushBack(&seq,4);PushFront(&seq,0);PopFront(&seq);Insert(&seq,2,6);ret?=?Find(&seq,6);printf("%d\n",ret);PrintSeqList(&seq); } void?Test3() {SeqList?seq;InitSeqList(&seq);PushBack(&seq,1);PushBack(&seq,2);PushBack(&seq,3);PushBack(&seq,4);PushBack(&seq,4);PushBack(&seq,2);PushBack(&seq,5);PushBack(&seq,6);PopBack(&seq);Erase(&seq,2);Remove(&seq,2);RemoveAll(&seq,4);PrintSeqList(&seq); } void?Test4() {SeqList?seq;int?pos;InitSeqList(&seq);PushBack(&seq,1);PushBack(&seq,2);PushBack(&seq,3);PushBack(&seq,4);PushBack(&seq,4);PushBack(&seq,5);ReverseList(&seq);SortList(&seq);pos?=?BinarySearch(&seq,2);printf("%d\n",pos);PrintSeqList(&seq); }void?Test5() {SeqList?seq;int?pos;InitSeqList(&seq);PushBack(&seq,1);PushBack(&seq,2);PushBack(&seq,3);PushBack(&seq,4);PushBack(&seq,4);PushBack(&seq,5);ReverseList(&seq); // SelectSortList(&seq); // Select_SortList(&seq);pos?=?BinarySearch(&seq,2);printf("%d\n",pos);PrintSeqList(&seq); } int?main() {//Test1();?//Test2();//Test3();//Test4();Test5();return?0; }


總結

以上是生活随笔為你收集整理的动态顺序表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。