線性表的鏈式表示與實現
阿新 • • 發佈:2019-02-05
#include<iostream> #include<malloc.h> using namespace std; typedef int ElemType; typedef int Status; #define OK 1 #define ERROR 0 typedef struct LNode{ ElemType data; struct LNode *next; }LNode,*LinkList; /*逆位序輸入n個元素的值,建立帶表頭結點的單鏈線性表L*/ void CreateList_L(LinkList &L,int n) { L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; for(int i=0;i!=n;i++) { LinkList p=(LinkList)malloc(sizeof(LNode)); cin>>p->data; p->next=L->next; L->next=p; } } /*當第i個元素存在時,其值賦給e並返回ok,否則返回error*/ Status GetElem_L(LinkList &L,int i,ElemType &e) { LinkList p=L->next; int n=1; while(n!=i&& p!=NULL) { p=p->next; n++; } if(!p) return ERROR; e=p->data; return OK; } /*在帶頭結點的單鏈表中,刪除第i個元素,並由e返回其值*/ Status ListDelete_L(LinkList &L,int i,ElemType &e) { LinkList p=L->next; LinkList q=L; int k=1; while(p!=NULL && k!=i) { k++; q=p; p=p->next; } if(p==NULL) return ERROR; e=p->data; q->next=p->next; free(p); return OK; } /*將兩個有序連結串列合併為一個有序連結串列*/ /*La和Lb的元素按值非遞減排列*/ /*在歸併兩個連結串列為一個連結串列時,不需要另建新表的結點空間,而只需要 將原來兩個連結串列中的結點之間的關係解除,重新按元素值非遞減的關係將所有結點連線成一個連結串列即可*/ void MergeList_L(LinkList &La,LinkList &Lb,LinkList &Lc) { LinkList pa=La->next; LinkList pb=Lb->next; LinkList pc=La; Lc=pc; while(pa && pb) { if(pa->data<=pb->data) { pc->next=pa; pc=pa; pa=pa->next; } else { pc->next=pb; pc=pb; pb=pb->next; } } if(pa) pc->next=pa; if(pb) pc->next=pb; } void print(LinkList &L) { LinkList p=L->next; cout<<"頭指標L->頭結點->"; while(p!=NULL) { cout<<p->data<<"->"; p=p->next; } cout<<"NULL"<<endl; } int main(){ /*cout<<"逆序建立一個連結串列,input n:"; int n; cin>>n; LinkList L; CreateList_L(L,n); print(L); ElemType e; if(GetElem_L(L,3,e)) cout<<e<<endl; else cout<<"doesn't exist!"<<endl; cout<<"刪除連結串列L的第3個位置的元素:"; if(ListDelete_L(L,3,e)) { cout<<e<<endl; cout<<"刪除之後為:"; print(L); } else cout<<"doesn't exist!"<<endl;*/ int n; cout<<"逆序建立一個連結串列(非遞減),input n:"; cin>>n; LinkList L1; CreateList_L(L1,n); print(L1); cout<<"逆序建立一個連結串列(非遞減),input n:"; cin>>n; LinkList L2; CreateList_L(L2,n); print(L2); cout<<"合併前面兩個連結串列(非遞減),input :"; LinkList L3; MergeList_L(L1,L2,L3); print(L3); return 0; }
#include<iostream> #include<malloc.h> using namespace std; typedef int ElemType; typedef int Status; #define OK 1 #define ERROR 0 #define MAX_SIZE 100 /*-------線性表的靜態單鏈表儲存結構----------*/ typedef struct{ ElemType data; int cur; }component,SLinkList[MAX_SIZE]; /*為了辨明陣列中哪些分量未被使用,解決的辦法是將所有未被使用過以及被刪除的分量 用遊標鏈成一個備用的連結串列,每當進行插入時,便可從備用連結串列上取得第一個結點作為待插入的新結點 反之,在刪除時將從連結串列中刪除下來的結點連結到備用連結串列上*/ /*將整個陣列空間初始化成一個連結串列*/ void InitSpace_SL(SLinkList &space) { /*將一維陣列space中各分量鏈成一個備用連結串列,space[0].cur為頭指標,0表示空指標*/ for(int i=0;i!=MAX_SIZE-1;i++) space[i].cur=i+1; space[MAX_SIZE-1].cur=0; } int Malloc_SL(SLinkList &space) { /*若備用空間連結串列非空,則返回分配的結點下標,否則返回0*/ int i=space[0].cur; if(space[0].cur) space[0].cur=space[i].cur;/*記錄備用空連結串列的第一個結點*/ return i; } void Free_SL(SLinkList &space,int k) { /*將下標為k的空閒結點回收到備用連結串列*/ space[k].cur=space[0].cur; space[0].cur=k; } void CreateList_SL(SLinkList &space,int n) { InitSpace_SL(space); int S=Malloc_SL(space);//生成頭結點 int r=S;//r指向S的當前最後結點 int m; for(int i=0;i!=n;i++) { m=Malloc_SL(space); cin>>space[m].data; space[r].cur=m; r=m; } space[r].cur=0; } /*在靜態單鏈線性表L中,查詢第1個值為e的元素 若找到,則返回它在L中的位序,否則返回0.*/ int LocateElem_SL(SLinkList space,ElemType e) { int i=space[1].cur; while(i && space[i].data!=e) i=space[i].cur; if(i==0) return 0; return i-1; } void print_SL(SLinkList space) { int i=space[1].cur; while(i) { cout<<space[i].data<<"->"; i=space[i].cur; } cout<<"NULL"<<endl; } /*刪除一個指定位置的元素*/ Status SListDelete_SL(SLinkList &space,int i,ElemType &e) { int k=space[1].cur; int r=k-1; int count=1; while(k && count!=i) { r=k; k=space[k].cur; count++; } if(k==0) return ERROR; e=space[k].data; space[r].cur=space[k].cur; Free_SL(space,k); return OK; } /*在指定的位置插入元素*/ Status SListInsert_SL(SLinkList &space,int i,ElemType e) { int k=space[1].cur; int count=1; int r=k-1; while(k && count!=i) { r=k; k=space[k].cur; count++; } if(k==0) return ERROR; int t=Malloc_SL(space); space[t].data=e; space[r].cur=t; space[t].cur=k; return OK; } int main(){ SLinkList space; cout<<"請輸入4個整數:"; CreateList_SL(space,4); print_SL(space); cout<<"在上述連結串列中,找到第一個等於2的數的位置:"; int t; if(t=LocateElem_SL(space,2)) cout<<t<<endl; else cout<<"不存在!"<<endl; cout<<"將上述位置的元素刪除後:"; int e; if(SListDelete_SL(space,t,e)) print_SL(space); else cout<<"ERROR!"<<endl; cout<<"將該刪除的元素還原後:"; if(SListInsert_SL(space,t,e)) print_SL(space); else cout<<"ERROR!"<<endl; return 0; }
#include<iostream> #include<stdlib.h> using namespace std; typedef int ElemType; typedef int Status; #define OK 1 #define ERROR 0 #define MAX_SIZE 100 /*---------線性表的雙向連結串列儲存結構-------*/ typedef struct DuLNode{ ElemType data; struct DuLNode *prior; struct DuLNode *next; }DuLNode,*DuLinkList; /*建立雙向連結串列*/ void CreateDuL(DuLinkList &L,int n) { L=(DuLinkList)malloc(sizeof(DuLinkList)); L->prior=L; L->next=L; DuLinkList t,p; p=L; for(int i=0;i!=n;i++) { t=(DuLinkList)malloc(sizeof(DuLinkList)); cin>>t->data; p->next=t; L->prior=t; t->prior=p; t->next=L; p=t; } } /*在帶頭結點的雙鏈迴圈線性表L中第i個位置之前插入元素e*/ Status ListInsert_DuL(DuLinkList &L,int i,ElemType e) { DuLinkList p=L->next; int count=1; while(p!=L && count!=i) { p=p->next; count++; } if(count==i) { DuLinkList t=(DuLinkList)malloc(sizeof(DuLinkList)); t->data=e; t->next=p; t->prior=p->prior; p->prior->next=t; p->prior=t; return OK; } return ERROR; } /*刪除帶頭結點的雙鏈迴圈線性表L的第i個元素*/ Status ListDelete_DuL(DuLinkList &L,int i,ElemType &e) { DuLinkList p=L->next; int count=1; while(p!=L && count!=i) { count++; p=p->next; } if(p==L) return ERROR; e=p->data; p->prior->next=p->next; p->next->prior=p->prior; //free(p);/*----------free的時候出錯,留待以後解決--------------*/ return OK; } /*輸出雙向連結串列*/ void print_DuL(DuLinkList L) { DuLinkList p=L->next; cout<<"L->頭結點->"; while(p!=L) { cout<<p->data<<"->"; p=p->next; } cout<<"NULL"<<endl; } int main(){ DuLinkList L; cout<<"輸入4個整數:"; CreateDuL(L,4); cout<<"輸出連結串列:"; print_DuL(L); // cout<<"在位置3之前,插入元素3後的連結串列為:"; // ListInsert_DuL(L,3,3); cout<<"在位置5之前,插入元素5後的連結串列為:"; ListInsert_DuL(L,5,5); print_DuL(L); cout<<"刪除上述操作後:"; ElemType e; if(ListDelete_DuL(L,5,e)) print_DuL(L); else cout<<"ERROR!"<<endl; return 0; }