/***資料結構<二>*鏈式儲存線性表(有頭結點)*/#include <iostream>#include <stdlib.h>using namespace std;typedef int ElemType;typedef struct LNode{ElemType data;struct LNode *next;}LNode,*LinkList;//LNode 結構體類型,LinkList 結構體指標類型//構造一個空的線性表int InitList(LinkList &head){head=(LinkList)malloc(sizeof(LNode));//head為頭指標,指向頭結點if(!head)return 1;head->next=NULL;//帶頭結點的鏈表return 0;}//銷毀線性表線性表void DestroyList(LinkList &head){LinkList p;if(head!=NULL)while (head->next!=NULL){p=head->next;head->next=head->next->next;free(p);//釋放p結點}free(head);//釋放頭結點head=NULL;//頭指標置空cout<<"銷毀鏈表操作成功!"<<endl;}//將head重設為空白表void ClearList(LinkList &head){LinkList p;if(head!=NULL)while (head->next!=NULL)//如果鏈表非空,則置空{p=head->next;head->next=head->next->next;free(p);}if(head->next==NULL)cout<<"置空鏈表操作成功!"<<endl;}//若head為空白表,則返回TRUE,否則返回FALSEint ListEmpty(LinkList head){if (head->next==NULL){return 1;}elsereturn 0;}//輸出head中資料元素的個數int ListLength(LinkList head){int num=0;LinkList p;if(head!=NULL)for(p=head;p->next != NULL;p=p->next)num++;return num;}//用e返回head中第i個資料元素的值int GetElem(LinkList head,int i,ElemType *e){int num;LinkList p;p=head;if(head != NULL)if(i>=1 && i<=ListLength(head))for(num=0;num<i;num++)p=p->next;*e=p->data;//cout<<"第 "<<i<<" 個資料元素的值為 "<< *e << endl;return *e;}//輸出head中第1個與e滿足相等關係的資料元素的位序。void LocateElem(LinkList head,ElemType e){int position=0;LinkList p=head;if(head!=NULL)while (p->next!=NULL){position++;if(e==p->next->data)//找到滿足條件的資料cout<<"元素 "<<e<<" 鏈表中的的位置為第 "<<position<<" 位"<<endl;p=p->next;}}//若cur_e是head的資料元素,且不是第一個,則用pre_e輸出它的前驅void PriorElem(LinkList head,ElemType cur_e,ElemType *pre_e){LinkList p=head;int count=0;if(head!=NULL)while (p->next!=NULL){count++;if(cur_e==p->next->data && cur_e!=head->data){break;}p=p->next;}if(count != 1 && count != 0 && p->next != NULL){cout<<cur_e<<" 的前驅結點為:"<< p->data<<endl;}else{cout<<"第一個元素沒有前驅!!!!"<<endl;}}//若cur_e是head的資料元素,且不是最後一個,則用next_e輸出它的後繼void NextElem(LinkList head,ElemType cur_e,ElemType *next_e){LinkList p=head;if(head!=NULL)while (p->next!=NULL){if(cur_e==p->next->data){break;}p=p->next;}if (p->next!=NULL&&p->next->next!=NULL){*next_e=p->next->next->data;cout<<cur_e<<" 的後繼結點為:"<<*next_e<<endl;}elsecout<<"最後一個元素沒有後繼!!!!"<<endl;}//在head中第i個位置之前插入新的資料元素e,head的長度加1void ListInsert(LinkList &head,int i,ElemType e){LinkList p=head;LinkList s;int j=0;if(head!=NULL)while (p!=NULL && j<i-1)//從頭結點開始尋找第i-1個節點{p=p->next;j++;}if(!p || j>i-1)//i小於1或者大於表長加1{cout<<"插入位置不合法!"<<endl;exit(0);}s=(LinkList)malloc(sizeof(*s));//產生新結點儲存要插入的資料結點s->data=e;s->next=p->next;p->next=s;}//刪除head的第i個資料元素,並用e返回其值,head的長度減1void ListDelete(LinkList &head,int i,ElemType *e){LinkList p=head;LinkList q;int j=0;while(p->next != NULL && j < i-1)//尋找第i個結點,並令p指向其前驅{p=p->next;j++;}if(!(p->next) || j > i-1){cout<<"刪除位元置不合理!"<<endl;exit(0);}q=p->next;*e=q->data;p->next=q->next;cout<<"刪除第 "<<i<<" 個資料 "<< *e <<" 成功"<<endl;free(q);//釋放刪除的結點}//依次對head的每個資料元素訪問void ListTraverse(LinkList head){LinkList p=head;if(head!=NULL){p=p->next;cout<<"輸出鏈表為:";while (p != NULL){cout<<p->data<<" ";p=p->next;}cout<<endl;}}void MergeList(LinkList &La,LinkList &Lb,LinkList &Lc){//已知單鏈線性表La,Lb的元素按值非遞減排列//歸併La和Lb得到新的單鏈線性表Lc,Lc的元素也按值非遞減排列InitList(La);InitList(Lb);InitList(Lc);int i,j;int a[4]={3,5,8,11},b[7]={2,6,8,9,11,15,20};LinkList pa,pb,pc,pnew;for(i = 1 ; i < 5 ; i++)ListInsert(La,i,a[i-1]);cout<<"線性表La的元素為:";pnew=La->next;while(pnew!=NULL){cout<<pnew->data<<" ";pnew=pnew->next;}cout<<endl;for(j = 1 ; j < 8 ; j++)ListInsert(Lb,j,b[j-1]);cout<<"線性表Lb的元素為:";pnew=Lb->next;while(pnew!=NULL){cout<<pnew->data<<" ";pnew=pnew->next;}cout<<endl;pa=La->next;pb=Lb->next;pc=La;Lc=pc;//用La的頭結點作為Lc的頭結點while(pa!=NULL && pb!=NULL){if(pa->data <= pb->data){pc->next=pa;pc=pa;pa=pa->next;}else{pc->next=pb;pc=pb;pb=pb->next;}}pc->next=pa ? pa : pb;//插入剩餘段free(Lb);cout<<"歸併後線性表Lc為:";pnew=Lc->next;while(pnew!=NULL){cout<<pnew->data<<" ";pnew=pnew->next;}cout<<endl;}int main(){LinkList L;ElemType j;int i;InitList(L);for(i=1,j=0;i<=10;i++)ListInsert(L,i,j++);ListTraverse(L);/*//測試ListDelete函數ElemType dNum;int dPosition;cout<<"請輸入刪除元素的位置:";cin>>dPosition;ListDelete(L,dPosition,&dNum);ListTraverse(L);*//*//測試PriorElem函數和NexElem函數ElemType cur_e,pre_e,next_e;cout<<"請輸入當前元素資料:";cin>>cur_e;PriorElem(L,cur_e,&pre_e);NextElem(L,cur_e,&next_e);*//*//測試LocateElem函數ElemType elem;cout<<"請輸入需要尋找的元素:";cin>>elem;LocateElem(L,elem);*//*//測試GetElem函數ElemType elem;int ePosition;cout<<"請輸入需要擷取元素的位置:";cin>>ePosition;GetElem(L,ePosition,&elem);*//*//測試ListLength函數int len;len=ListLength(L);cout<<"鏈表長度為:"<<len<<endl;*//*//測試ListEmpty函數int re =ListEmpty(L);cout<<re<<endl;//返回0表示非空,1表示鏈表為空白*//*//測試ClearList函數ClearList(L);*//*//測試DestroyList函數DestroyList(L);*//*//測試MergeList函數LinkList La,Lb,Lc;MergeList(La,Lb,Lc);*/return 0;}