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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【动态顺序表】 c语言的动态顺序表

發布時間:2025/3/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【动态顺序表】 c语言的动态顺序表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

動態順序表: 容量不夠時 自動增容(靜態順序表的改進)

? ? ? ? ? ??

動態順序表的結構:

typedef int DataType;

typedef struct SeqList

{

DataType* _array; ? //指向數據塊的指針

size_t _size; ? ? ? //有效數據個數

size_t _capacity; ? //容量

}SeqList;

增容:

void _CheckCapacity(SeqList* pSeq)

{

if (pSeq->_size >= pSeq->_capacity)

{

//防止開始空間為0,而容量一直為0,所以多開辟一些空間。

pSeq->_capacity = 2 * pSeq->_capacity + 3;

pSeq->_array = (DataType *)realloc(pSeq->_array, pSeq- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? >_capacity*sizeof(DataType));

? ? ?? }

}

//順序表中數據為空可用提示語句給出或者assert報錯

void?InitSeqList(SeqList*?pSeq)?//初始化 {assert(pSeq);//memset(pSeq->_array,?0,?sizeof(DataType)*MAX_SIZE);pSeq->_array?=?NULL;pSeq->_size?=?0;pSeq->_capacity?=?0; } void?PrintSeqList(SeqList*?pSeq) {assert(pSeq);for?(size_t?i?=?0;?i<pSeq->_size;?++i){printf("%d?",?pSeq->_array[i]);}printf("NULL?\n"); }void?_CheckCapacity(SeqList*?pSeq) {if?(pSeq->_size?>=?pSeq->_capacity){//防止開始空間為0,而容量一直為0,所以多開辟一些空間。pSeq->_capacity?=?2?*?pSeq->_capacity?+?3;pSeq->_array?=?(DataType?*)realloc(pSeq->_array,?pSeq->_capacity*sizeof(DataType));} } void?PushBack(SeqList*?pSeq,?DataType?x)?//尾插 {assert(pSeq);_CheckCapacity(pSeq);pSeq->_array[pSeq->_size++]?=?x; } void?PopBack(SeqList*?pSeq) {assert(pSeq);if?(pSeq->_size){pSeq->_array[pSeq->_size]?=?NULL;pSeq->_size--;} } void?PushFront(SeqList*?pSeq,?DataType?x)??//頭插 {assert(pSeq);_CheckCapacity(pSeq); //注意此處應用int而不能用size_t,否則會一直循環.size_t永遠>=0for?(int?i?=?pSeq->_size?-?1;?i?>=?0;?--i)??{?????????????????????????????????????????pSeq->_array[i?+?1]?=?pSeq->_array[i];}pSeq->_array[0]?=?x;pSeq->_size++; } void?PopFront(SeqList*?pSeq) {assert(pSeq);if?(pSeq->_size?<=?0){printf("SeqList?is?empty\n");return;}//assert(pSeq->_size);for?(size_t?i?=?0;?i?<?pSeq->_size;?++i){pSeq->_array[i]?=?pSeq->_array[i?+?1];}pSeq->_size--;}void?Insert(SeqList*?pSeq,?size_t?pos,?DataType?x)??//增 {assert(pSeq);assert(pos?<=?pSeq->_size);_CheckCapacity(pSeq);for?(int?i?=?pSeq->_size?-?1;?i?>=?(int)pos;?--i)?????????//?注意size_t永遠>=0{pSeq->_array[i?+?1]?=?pSeq->_array[i];}pSeq->_array[pos]?=?x;pSeq->_size++; } int?Find(SeqList*?pSeq,?size_t?pos,?DataType?x) {assert(pSeq);for?(size_t?i?=?pos;?i?<?pSeq->_size;?++i){if?(pSeq->_array[i]?==?x)return?i;}return?-1; } void?Erase(SeqList*?pSeq,?size_t?pos)??//刪 {assert(pSeq);assert(pos<pSeq->_size);for?(size_t?i?=?pos?+?1;?i?<?pSeq->_size;?++i){pSeq->_array[i?-?1]?=?pSeq->_array[i];}pSeq->_size--; }int?Remove(SeqList*?pSeq,?DataType?x) {assert(pSeq);int?pos?=?Find(pSeq,?0,?x);if?(pos?!=?-1){Erase(pSeq,?pos);}return?pos; } void?RemoveAll(SeqList*?pSeq,?DataType?x)??//清除 {int?count?=?0;assert(pSeq);for?(size_t?i?=?0;?i?<pSeq->_size;?i++){if?(pSeq->_array[i]?==?x){count++;}else{pSeq->_array[i?-?count]?=?pSeq->_array[i];}}pSeq->_size?-=?count; }




測試:

//PushBack??PopBack void?Test1() {SeqList?seq;InitSeqList(&seq);PushBack(&seq,?1);PushBack(&seq,?2);PushBack(&seq,?3);PushBack(&seq,?4);PushBack(&seq,?5);PushBack(&seq,?6);PrintSeqList(&seq);PopBack(&seq);PopBack(&seq);PopBack(&seq);PrintSeqList(&seq);PopBack(&seq);PopBack(&seq);PrintSeqList(&seq);} //PushBack?PopBack void?Test2() {SeqList?seq;InitSeqList(&seq);PushFront(&seq,?1);PushFront(&seq,?2);PushFront(&seq,?3);PushFront(&seq,?4);PrintSeqList(&seq);PushFront(&seq,?0);PushFront(&seq,?-1);PrintSeqList(&seq);PopFront(&seq);PopFront(&seq);PopFront(&seq);PrintSeqList(&seq);PopFront(&seq);PopFront(&seq);PopFront(&seq);PopFront(&seq);PrintSeqList(&seq); } //?Insert/Find/Erase void?Test3() {SeqList?seq;InitSeqList(&seq);PushBack(&seq,?1);PushBack(&seq,?2);PushBack(&seq,?4);PushBack(&seq,?5);PrintSeqList(&seq);int?tmp?=?Find(&seq,?0,?2);printf("%d\n",?tmp);Insert(&seq,?tmp,?100);Insert(&seq,?2,?10);Insert(&seq,?2,?3);PrintSeqList(&seq);//刪除tmp?=?Find(&seq,?0,?3);printf("%d\n",?tmp);Erase(&seq,?tmp);PrintSeqList(&seq);//添加?越界情況??tmp?=?Find(&seq,?0,?1);printf("%d\n",?tmp);Insert(&seq,?tmp,?100);PrintSeqList(&seq);} //Remove??RemoveAll void?Test4() {SeqList?seq;InitSeqList(&seq);PushBack(&seq,?1);PushBack(&seq,?2);PushBack(&seq,?3);PushBack(&seq,?4);PushBack(&seq,?2);PrintSeqList(&seq);/*Remove(&seq,?3);PrintSeqList(&seq);*/RemoveAll(&seq,?2);PrintSeqList(&seq); }


轉載于:https://blog.51cto.com/1536262434/1753702

總結

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

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