資料結構- - 線性表- -順序表_完善
阿新 • • 發佈:2018-12-30
豐富了前邊的功能,更加完善。
#include <iostream> #include <stdlib.h> #define LIST_INIT_SIZE 100 //線性表儲存空間的初始分配量 #define LISTINCREMENT 10 //線性表儲存空間的分配增量 using namespace std; const int OVERFLOW = -2; typedef int Status; typedef int ElemType;//將ElemType型別為int typedef struct { ElemType *elem; int length; //當前表長 int listsize; //當前分配的儲存容量,單位為(ElemType) }SqList; Status InitList_Sq(SqList *L) //構建一個空連結串列 { L->elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!L->elem) exit(OVERFLOW); //記憶體分配失敗 L->length=0; L->listsize=LIST_INIT_SIZE; return 1; } /* Status InitList _Sq(SqList &L) { L.elem=new ElemType[LIST_INIT_SIZE]; L.lenght=0; L.listsize=LIST_INIT_SIZE; return 1; }*/ /* void DestroyList(SqList &L) { if (L.elem) delete[ ] L.elem; //釋放儲存空間 L.length=0; L.listsize=0; }*/ void DestroyList(SqList *L) { if(L->elem) free(L->elem); L->length=0; L->listsize=0; } void ClearList (SqList &L) { L.length=0; } int Getlength(SqList &L) { return L.length; } bool IsEmpty(SqList &L) { if(L.length==0) return true; else return false; } Status GetElem(SqList &L,int i,ElemType &e) { if(i<1||i>L.length) return 0; e=L.elem[i-1]; return 1; } Status ListInsert_Sq(SqList &L,int i,ElemType e){ if(i<1||i>L.length+1) return 0; if(L.length==L.listsize) { ElemType *newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); L.elem=newbase; L.listsize+=LISTINCREMENT; } ElemType* q=&L.elem[i-1]; for(ElemType *p=&L.elem[L.length-1];p>=q;--p) *(p+1)=*p; *q=e; ++L.length; return 1; } Status ListDelete_Sq(SqList &L,int i,ElemType &e) { if(i<1||i>L.length) return 0; ElemType *q=&L.elem[i-1]; e=*q; ElemType *p=&L.length-1; for(*q=*(q+1);q<=p;q++) *(q-1)=*q; L.length--; return 1; } int show(SqList &L){ if(L.length==0) { cout<<"線性表內沒有元素"<<endl; return 0; } cout<<"線性表裡的元素有:"<<endl; ElemType *p=&L.elem[L.length-1]; for(ElemType *i=&L.elem[0];i<=p;i++) cout<<*i<<" "; cout<<endl; return 1; } Status List_pre(SqList &L,int i,ElemType &e) { if(i==1) return 0; else { e=L.elem[i-2]; return 1; } } Status List_flo(SqList &L,int i,ElemType &e) { if(i==L.length) return 0; else{ e=L.elem[i]; return 1; } } void show_help() { cout<<"1----清空線性表"<<endl; cout<<"2----判斷線性表是否為空"<<endl; cout<<"3----求線性表長度"<<endl; cout<<"4----獲取線性表指定位置元素"<<endl; cout<<"5----求前驅"<<endl; cout<<"6----求後繼"<<endl; cout<<"7----線上性表指定位置插入元素"<<endl; cout<<"8----刪除線性表指定位置元素"<<endl; cout<<"9----顯示線性表"<<endl; cout<<" 退出,輸出一個負數!"<<endl; } int main() { int operate_code; SqList L; InitList_Sq(&L); show_help(); int i,x; ElemType e; //定義線性表變數,如SqList L; //呼叫初始化線性表函式,如 Init_List(L); while(1) { cout<<"請輸入操作程式碼:"; cin>>operate_code; if(operate_code==1) { ClearList (L); } else if(operate_code==2) { if(IsEmpty (L)) cout<<"The List is empty"<<endl; else cout<<"The List is not empty"<<endl; } else if(operate_code==3) { cout<<"The length of list is: "<<Getlength(L)<<endl; } else if(operate_code==4) { cout<<"請輸入你想要查詢指定位置的元素:"<<endl; cin>>i; GetElem(L,i,x); cout<<"在"<<i<<"位置的元素為: "<<x<<endl; } else if(operate_code==5) { cout<<"請輸入元素:"<<endl; cin>>i; List_pre(L,i,x); if(!List_pre(L,i,x)) cout<<"這個元素沒有前驅。"; else cout<<"這個元素的前驅為:"<<x<<endl; } else if(operate_code==6) { cout<<"請輸入元素:"<<endl; cin>>i; List_flo(L,i,x); if(!List_flo(L,i,x)) cout<<"這個元素沒有後繼。"; else cout<<"這個元素的後繼為:"<<x<<endl; } else if(operate_code==7) { cout<<"請輸入插入的位置及元素:"<<endl; cin>>i>>e; ListInsert_Sq(L,i,e); } else if(operate_code==8) { cout<<"請輸入你要刪除哪個位置的元素:"<<endl; cin>>i; ListDelete_Sq(L,i,e); } else if(operate_code==9) { show(L); } else if(operate_code<0) { break; } else { cout<<"\n操作碼錯誤!!!"<<endl; show_help(); } } //呼叫銷燬線性表函式,如 Destroy_List(L); DestroyList(&L); return 0; }