C++实现线性顺序表的初始化,插入,删除,销毁,清空等功能
生活随笔
收集整理的這篇文章主要介紹了
C++实现线性顺序表的初始化,插入,删除,销毁,清空等功能
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
C++實(shí)現(xiàn)線性順序表的初始化,插入,刪除,銷毀,清空等功能
以下代碼主要嚴(yán)蔚參考敏主編的《數(shù)據(jù)結(jié)構(gòu)》(C語言)編寫
//線性順序表 #include <iostream> #define MAX_INTT_LIST 100 #define Elemtype int #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 typedef int Status;using namespace std; typedef struct List {Elemtype *Elem;int Length;int Listsize;}; Status InitList( List &L) //初始化一個(gè)線性表; {L.Elem=new Elemtype[MAX_INTT_LIST];if(!L.Elem){return ERROR;}else{L.Length=0;L.Listsize=MAX_INTT_LIST ;return OK;} } void DestoryList(List &L)//銷毀一個(gè)線性表 {delete[] L.Elem;L.Length=0;L.Listsize=0; } void ClearList(List &L)//清空一個(gè)線性表 {L.Length=0; } int ListLength(List L)//返回線性表的長(zhǎng)度 {return L.Length; } Status GetElem (List L,int i, Elemtype &e)//用e返回線性表L中第i個(gè)數(shù)據(jù)元素的值 {if(i>ListLength(L)||i<0){return FALSE;}else{e=L.Elem[i];return TRUE;}} Status ListEmpty(List L)//查看線性表L是否為空 {if(L.Length==0)return TRUE;elsereturn FALSE; } Status compare(Elemtype a,Elemtype b) {if(a==b)return TRUE;elsereturn FALSE; } int LocateElem(List L, Elemtype e,Status (*compare)(Elemtype a,Elemtype b))//返回線性表中第一個(gè)與e滿足compare()關(guān)系的數(shù)據(jù)元素的位序 {int i=1;while(i<=L.Length&&!(*compare)(L.Elem[i],e))i++;if(i<=L.Length)return i;elsereturn 0; } void PriorElem(List L, Elemtype cur_e, Elemtype &pre_e)//在線性表L中求數(shù)據(jù)元素cur_e的前驅(qū) {for(int i=1; i<=L.Length; i++){if(L.Elem[i]==cur_e&&i!=1){pre_e=L.Elem[i-1];break;}}} void NextElem(List L,Elemtype cur_e,Elemtype &next_e)//在線性表L中求數(shù)據(jù)元素cur_e的后繼 {for(int i=1; i<=L.Length; i++){if(L.Elem[i]==cur_e&&i!=L.Length){next_e=L.Elem[i+1];break;}} } void ListInsert(List &L,int i,Elemtype e)//在線性表L中插入一個(gè)新的元素 {if(i<=L.Length+1&&i>=1){for(int k=L.Length+1; k>i; k--){L.Elem[k]=L.Elem[k-1];}L.Elem[i]=e;L.Length++;} } void ListDelete(List &L,int i,Elemtype &e)//在線性表L中刪除第i個(gè)數(shù)據(jù)元素 {if(i<=L.Length&&i>=1&&!ListEmpty(L)){e=L.Elem[i];for(int k=i; k<=L.Length; k++){L.Elem[k]=L.Elem[k+1];}L.Length--;} }Elemtype visit(List L,int i) {if(L.Elem[i]!=NULL)return L.Elem[i];elsereturn FALSE; } Elemtype ListTraverse(List L,Elemtype (*vist)(List L,int i))//遍歷線性表 {for(int i=1; i<=L.Length; i++){if(vist(L,i))return L.Elem[i];elsereturn FALSE;} } int main() {List L;int e=0;Elemtype pre_e=0,next_e=0;InitList(L);cout<<ListEmpty(L)<<endl;ListInsert(L,1,6);ListInsert(L,2,7);ListInsert(L,3,8);for(int i=1; i<=L.Length; i++){cout<<L.Elem[i]<<" ";}cout<<endl;GetElem(L,2,e);cout<<e<<endl;PriorElem(L,7,pre_e);cout<<pre_e<<endl;NextElem(L,7,next_e);cout<<next_e<<endl;cout<<LocateElem(L,8,(*compare))<<endl;ListDelete(L,3,e);cout<<e<<endl;for(int i=1; i<=L.Length; i++){cout<<L.Elem[i]<<" ";}cout<<endl;}最近在提前預(yù)習(xí)數(shù)據(jù)結(jié)構(gòu)這門課,在閱讀嚴(yán)蔚敏的《數(shù)據(jù)結(jié)構(gòu)》時(shí)發(fā)現(xiàn)書上的這一部分并未給出詳細(xì)代碼,于是在這里寫下自己寫的代碼,希望能夠給同樣學(xué)習(xí)這方面知識(shí)的小伙伴一點(diǎn)參考。
若以上代碼有任何錯(cuò)誤或可以改進(jìn)的地方歡迎在評(píng)論區(qū)指出并討論。
感謝你的閱讀。
總結(jié)
以上是生活随笔為你收集整理的C++实现线性顺序表的初始化,插入,删除,销毁,清空等功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国工程院院士李伯虎:面向智慧制造云的仿
- 下一篇: 宝鲲财经:个人外汇交易如何管理资金