您好,登錄后才能下訂單哦!
#include<iostream> //c++頭文件 using namespace std; #define LIST_INIT_SIZE 20 //預(yù)定義常量 #define LISTINCREMENT 5 #define OK 1 #define ERROR 0 typedef int ElemType; //定義類型 typedef int Status; typedef struct //線性表結(jié)構(gòu)體定義 { ElemType *elem; int length; int listsize; }SqList; Status InitList_Sq(SqList &L) //創(chuàng)建空的線性表 { L.elem=new ElemType[LIST_INIT_SIZE]; //申請空間 L.length=0; //令表中數(shù)據(jù)長度為零 L.listsize=LIST_INIT_SIZE; //將所申請的空間給予元素空間 return OK; } Status CreateList_Sq(SqList &L,int n) //創(chuàng)建一個線性表 { int i; InitList_Sq(L); //創(chuàng)建空表 if(n>L.listsize) //保證空間足夠 { //續(xù)開辟空間 } L.length=n; cout<<"please input "<<n<<" numbers:"; for(i=0;i<L.length;i++) //輸入數(shù)據(jù)元素 cin>>L.elem[i]; return OK; } Status OutputList_Sq(SqList L) //輸出線性表 { int i; cout<<"The list is:"; for(i=0;i<L.length;i++) cout<<L.elem[i]<<" "; cout<<endl; return OK; } Status ListInsert_Sq(SqList &L,int i,ElemType e) //插入元素(前插法) { int j; if(i<1||i>L.length+1) // (1)判斷插入元素下標(biāo)i的合法性(1---L.length+1) return ERROR; if(L.length>=L.listsize) //判斷空間是否夠用 { //續(xù)開辟空間 } for(j=L.length-1;j>=i-1;j--) // (2) 下標(biāo)L.length-1----i-1的元素,后移一個位置 L.elem[j+1]=L.elem[j]; L.elem[i-1]=e; // (3) 插入e(下標(biāo)i-1) L.length++; // (4) 數(shù)據(jù)元素長度加一 return OK; } Status ListDelete_Sq(SqList &L,int i,ElemType &e) //刪除元素,并用e返回該數(shù)據(jù) { int *p,*q; if(i<1||i>L.length) // (1) 判斷插入元素下標(biāo)i的合法性(1---L.length+1) return ERROR; p=&(L.elem[i-1]); // (2) 取第i個元素的地址,并用e取其中數(shù)據(jù) e=*p; q=L.elem+L.length-1; //最后(表尾)元素的位置 for(++p;p<=q;++p) *(p-1)=*p; // (3) 元素依次左移 --L.length; // (4) 元素長度減一 return e; } Status ListLength_Sq(SqList &L) //求元素長度 { int i,p=0; for(i=0;i<L.length;i++) if(L.elem[i]!='\0') p=p+1; cout<<"The length is "<<p<<"\n"; return OK; } Status PriorElem_Sq(SqList &L,int cur_e,ElemType &pre_e) // 求第cur_e個數(shù)據(jù)元素的前驅(qū),用pre_e返回 { int i,*p=0; for(i=0;i<L.length;i++) { if(cur_e==1) //首個元素?zé)o前驅(qū) { cout<<"不存在前驅(qū) "<<endl; break; } else if(L.elem[i]==L.elem[cur_e-1]) { p=&(L.elem[i-1]); pre_e=*p; cout<<"該數(shù)前驅(qū)為 "<<pre_e<<"\n"; } } return OK; } Status NextElem_Sq(SqList &L,int cur_e,ElemType &next_e) //求第cur_e個數(shù)據(jù)元素的后繼,用next_e返回 { int i,*p=0; for (i=0;i<L.length;i++){ if(cur_e==L.length) //最后一個元素?zé)o后繼 { cout<<"不存在后繼 "<<endl; break; } else if(L.elem[i]==L.elem[cur_e-1]) { p=&(L.elem[i+1]); next_e=*p; cout<<"該數(shù)后繼為 "<<next_e<<"\n"; } } return OK; } Status GetElem(SqList L,int i,ElemType &e) //獲取第i個元素 { int *p=0; if(i<1||i>L.length) return ERROR; else{ p=&(L.elem[i-1]); e=*p; } return e; } Status DestroyList(SqList &L) //銷毀線性表 { free(L.elem); //釋放空間 L.elem=NULL; L.length=0; L.listsize=0; cout<< "線性表已銷毀! "<<endl; return OK; } Status equal(SqList L,int c1,int c2) //比較兩個元素是否相等 { if(L.elem[c1]==L.elem[c2]) cout<<"相等"<<endl; else cout<<"不相等"<<endl; return OK; } int main() { SqList L1; ElemType e; ElemType pre_e,next_e; CreateList_Sq(L1,5); //創(chuàng)建線性表 ListInsert_Sq(L1,3,0); // 在第三個元素插入 cout<<"插入后的線性表:"<<endl; OutputList_Sq(L1); cout<<"刪除的元素為"<<ListDelete_Sq(L1,3,e)<<endl; //刪除第三個元素 OutputList_Sq(L1); cout<<"第三個元素為"<<GetElem(L1,3,e)<<endl; //獲取第三個元素 PriorElem_Sq(L1,3,pre_e); //求第三個元素的前驅(qū) NextElem_Sq(L1,3,next_e); //求第三個元素的后繼 cout<<"判斷第二第三個元素是否相等"<<endl; //判斷元素是否相等 equal( L1, 2, 3); ListLength_Sq(L1); //求線性表的長度 DestroyList(L1); //銷毀線性表 return 1; }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。