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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++实现静态顺序表的增删查改以及初始化

發布時間:2023/12/9 c/c++ 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++实现静态顺序表的增删查改以及初始化 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C++實現靜態順序表的增刪查改


順序表:用一段地址連續的存儲單元依s次存儲數據元素的線性結構,是線性表的一種。


//SeqList.h#pragma?once#include?<assert.h> #include?<string.h>#define?MAX_SIZE?5 typedef?int?DataType;//定義順序表結構體 typedef?struct?SeqList {DataType?array[MAX_SIZE];??//數據塊數組size_t?size;???????????????//當前有效數據個數 }SeqList;//有關順序表函數的聲明 void?InitSeqList(SeqList*?pSeq); void?PushBack(SeqList*?pSeq,DataType?x); void?PopBack(SeqList*?pSeq); void?PushFront(SeqList*?pSeq,DataType?x); void?PopFront(SeqList*?pSeq); void?PrintSeqList(SeqList*?pSeq); void?Insert(SeqList*?pSeq,?size_t?pos,?DataType?x); int?Find(SeqList*?pSeq,?DataType?x); void?Erase(SeqList*?pSeq,?size_t?pos); void?Remove(SeqList*?pSeq,?DataType?x); void?RemoveAll(SeqList*?pSeq,?DataType?x);//初始化 void?InitSeqList(SeqList*?pSeq) {assert(pSeq);memset(pSeq->array,?0,?sizeof(DataType)*MAX_SIZE);pSeq->size?=?0; }//尾插 void?PushBack(SeqList*?pSeq,?DataType?x) {assert(pSeq);if?(pSeq->size?>=?MAX_SIZE){cout?<<?"The?SeqList?is?Full!"?<<?endl;return;}pSeq->array[pSeq->size++]?=?x; }//尾刪 void?PopBack(SeqList*?pSeq) {assert(pSeq);if?(pSeq->size?<=?0){cout?<<?"The?SeqList?is?Empty!"?<<?endl;return;}pSeq->array[--pSeq->size]?=?0; }//頭插 void?PushFront(SeqList*?pSeq,?DataType?x) {assert(pSeq);DataType?begin?=?pSeq->size?-?1;if?(pSeq->size?>=?MAX_SIZE){cout?<<?"The?SeqList?is?Full!"?<<?endl;return;}for?(;begin?>=?0;--begin){pSeq->array[begin?+?1]?=?pSeq->array[begin];}pSeq->array[0]?=?x;++pSeq->size; }//頭刪 void?PopFront(SeqList*?pSeq) {assert(pSeq);DataType?begin?=?0;if?(pSeq->size?<=?0){cout?<<?"The?SeqList?is?Empty!"?<<?endl;return;}for?(;begin?<=?pSeq->size;++begin){pSeq->array[begin]?=?pSeq->array[begin?+?1];}pSeq->array[pSeq->size]?=?0;--pSeq->size; }//打印 void?PrintSeqList(SeqList*?pSeq) {assert(pSeq);DataType?i?=?0;for?(;i?<?pSeq->size;++i){cout?<<?pSeq->array[i]<<"?";}cout?<<?endl; }//修改某個位置上的數據 void?Insert(SeqList*?pSeq,?size_t?pos,?DataType?x) {assert(pSeq);DataType?begin?=?pSeq->size;if?(pos?>=?pSeq->size){cout?<<?"The?pos?is?wrong!"?<<?endl;return;}if?(pSeq->size?>=?MAX_SIZE){cout?<<?"The?SeqList?is?Full!"?<<?endl;return;}for?(;begin?>=?pos;--begin){pSeq->array[begin]?=?pSeq->array[begin?-?1];}pSeq->array[pos?-?1]?=?x;++pSeq->size; }//查找 int?Find(SeqList*?pSeq,?DataType?x) {assert(pSeq);int?i?=?0;for?(;i?<?pSeq->size;++i){if?(pSeq->array[i]?==?x){return?i;}}return?-1;????//表示沒有找到x }//刪除某個位置上的數據 void?Erase(SeqList*?pSeq,?size_t?pos) {assert(pSeq);//DataType?begin?=?pSeq->size?-1;if?(pos?>=?pSeq->size){cout?<<?"The?pos?is?wrong!"?<<?endl;return;}if?(pSeq->size?<=?0){cout?<<?"The?SeqList?is?Empty!"?<<?endl;return;}for?(;pos?<?pSeq->size;++pos){pSeq->array[pos]?=?pSeq->array[pos?+?1];}--pSeq->size; }//刪除順序表中第一個值為X的數據 void?Remove(SeqList*?pSeq,?DataType?x) {assert(pSeq);int?pos?=?0;pos?=?Find(pSeq,?x);if?(pos?!=?-1){Erase(pSeq,?pos);} }//刪除順序表中所有值為X的數據 void?RemoveAll(SeqList*?pSeq,?DataType?x) {assert(pSeq);int?pos?=?0;pos?=?Find(pSeq,?x);while?(pos?!=?-1){Erase(pSeq,?pos);pos?=?Find(pSeq,?x);} }//Test.cpp??#define?_CRT_SECURE_NO_WARNINGS?1#include?<iostream> using?namespace?std;#include?"SeqList.h"//測試尾插 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); }//測試頭插頭刪 void?Test2() {SeqList?seq;InitSeqList(&seq);PushBack(&seq,?1);PushBack(&seq,?2);PushBack(&seq,?3);PushBack(&seq,?4);PushFront(&seq,?0);PushFront(&seq,?-1);PrintSeqList(&seq);PopBack(&seq);PopBack(&seq);PopBack(&seq);PopBack(&seq);PopBack(&seq);PopBack(&seq);PrintSeqList(&seq); }//測試修改某個位置上的數據 void?Test3() {SeqList?seq;InitSeqList(&seq);PushBack(&seq,?1);PushBack(&seq,?2);PushBack(&seq,?4);PushBack(&seq,?5);Insert(&seq,?3,?3);PrintSeqList(&seq); }//測試查找和刪除 void?Test4() {SeqList?seq;InitSeqList(&seq);PushBack(&seq,?1);PushBack(&seq,?2);PushBack(&seq,?3);PushBack(&seq,?4);PrintSeqList(&seq);int?ret?=?Find(&seq,?2);cout?<<?"pos:"?<<?ret?<<?endl;Erase(&seq,?3);PrintSeqList(&seq); }//測試刪除順序表中第一個值為x的數據 void?Test5() {SeqList?seq;InitSeqList(&seq);PushBack(&seq,?1);PushBack(&seq,?2);PushBack(&seq,?3);PushBack(&seq,?4);PrintSeqList(&seq);Remove(&seq,?2);PrintSeqList(&seq); }int?main() {//Test1();//Test2();//Test3();//Test4();Test5();system("pause");return?0; }

有問題的地方還請多多指教


轉載于:https://blog.51cto.com/clown5/1753486

總結

以上是生活随笔為你收集整理的C++实现静态顺序表的增删查改以及初始化的全部內容,希望文章能夠幫你解決所遇到的問題。

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