資料結構課程上機實驗題(二)
阿新 • • 發佈:2019-02-13
2.1,編寫一個程式exp2-1.cpp,實現順序表的各種基本運算功能(假設順序表的元素型別為char),並在此基礎上完成如下功能:
(1)初始化順序表L;
(2)採用尾插法依次插入元素a,b,c,d,e;
(3)輸出順序表L;
(4)輸出順序表L長度;
(5)判斷順序表L是否為空;
(6)輸出順序表L的第三個元素;
(7)輸出元素a的位置;
(8)在第四個元素位置上插入元素f;
(9)輸出順序表;
(10)刪除L的第三個元素;
(11)輸出順序表L;
(12)釋放順序表L;
程式碼如下:
#include <iostream> #include <cstdio> #include <malloc.h> using namespace std; #define MaxSize 50 typedef struct{ char data[MaxSize]; int length; }SqList; void CreateList(SqList *&L,char a[],int n) { L=(SqList *)malloc(sizeof(SqList)); for (int i=0;i<n;i++) { L->data[i]=a[i]; } L->length=n; } void InitList(SqList *&L) { L=(SqList *)malloc(sizeof(SqList)); L->length=0; } void DestroyList(SqList *&L) { free(L); } bool ListEmpty(SqList *L) { return (L->length==0); } int ListLength(SqList *L) { return (L->length); } void DispList(SqList *L) { int i; for (i=0;i<L->length;i++) { printf("%c ",L->data[i]); } printf("\n"); } bool GetElem(SqList *L,int i,char &e) { if (i<1|| i>L->length) return false; e=L->data[i-1]; return true; } int LocateElem(SqList *L,char e) { int i=0; while (i<L->length&&L->data[i]!=e) { i++; } if (i>=L->length) return 0; else return i+1; } bool ListInsert(SqList *&L,int i,char e) { int j; if (i<1||i>L->length+1) return false; i--; for (j=L->length;j>i;j--) { L->data[j]=L->data[j-1]; } L->data[i]=e; L->length++; return true; } bool ListDelete(SqList *&L,int i,char &e) { int j; if (i<1||i>L->length) { return false; } i--; e=L->data[i]; for (j=i;j<L->length-1;j++) L->data[j]=L->data[j+1]; L->length--; return true; } int main() { SqList *L; char e; cout<<"(1)初始化順序表L"<<endl; InitList(L); cout<<"(2)依次採用尾插法插入a,b,c,d,e元素"<<endl; ListInsert(L,1,'a'); ListInsert(L,2,'b'); ListInsert(L,3,'c'); ListInsert(L,4,'d'); ListInsert(L,5,'e'); cout<<"(3)輸出順序表L:"; DispList(L); cout<<"(4)順序表L長度="<<ListLength(L)<<endl; cout<<"(5)順序表L為"<<(ListEmpty(L)?"空":"非空")<<endl; GetElem(L,3,e); cout<<"(6)順序表L的第3個元素="<<e<<endl; cout<<"(7)元素a的位置="<<LocateElem(L,'a')<<endl; cout<<"(8)在第4個元素位置上插入f元素"<<endl; ListInsert(L,4,'f'); cout<<"(9)輸出順序表L:"; DispList(L); cout<<"(10)刪除L的第3個元素"<<endl; ListDelete(L,3,e); cout<<"(11)輸出順序表L:"; DispList(L); cout<<"(12)釋放順序表L"<<endl; DestroyList(L); return 0; }
2.編寫程式exp2-2.cpp,實現單鏈表的各種基本運算(假設單鏈表的元素型別為char),並在此基礎上完成如下功能;
(1)初始化單鏈表h;
(2)採用尾插法依次插入元素a,b,c,d,e;
(3)輸出單鏈表h;
(4)輸出單鏈表h長度;
(5)判斷單鏈表h是否為空;
(6)輸出單鏈表h的第三個元素;
(7)輸出元素a的位置;
(8)在第四個元素位置上插入元素f;
(9)輸出單鏈表h;
(10)刪除h的第三個元素;
(11)輸出單鏈表h;
(12)釋放單鏈表h;
程式碼如下:
#include <iostream> #include <cstdio> #include <malloc.h> using namespace std; typedef struct LNode { char data; struct LNode *next; } LinkList; void InitList(LinkList *&L) { L=(LinkList *)malloc(sizeof(LinkList)); L->next=NULL; } void DestroyList(LinkList *&L) { LinkList *p=L,*q=p->next; while (q!=NULL) { free(p); p=q; q=p->next; } free(p); } bool ListEmpty(LinkList *L) { return(L->next==NULL); } int ListLength(LinkList *L) { LinkList *p=L;int i=0; while (p->next!=NULL) { i++; p=p->next; } return(i); } void DispList(LinkList *L) { LinkList *p=L->next; while (p!=NULL) { printf("%c ",p->data); p=p->next; } printf("\n"); } bool GetElem(LinkList *L,int i,char &e) { int j=0; LinkList *p=L; while (j<i && p!=NULL) { j++; p=p->next; } if (p==NULL) return false; else { e=p->data; return true; } } int LocateElem(LinkList *L,char e) { int i=1; LinkList *p=L->next; while (p!=NULL && p->data!=e) { p=p->next; i++; } if (p==NULL) return(0); else return(i); } bool ListInsert(LinkList *&L,int i,char e) { int j=0; LinkList *p=L,*s; while (j<i-1 && p!=NULL) { j++; p=p->next; } if (p==NULL) return false; else { s=(LinkList *)malloc(sizeof(LinkList)); s->data=e; s->next=p->next; p->next=s; return true; } } bool ListDelete(LinkList *&L,int i,char &e) { int j=0; LinkList *p=L,*q; while (j<i-1 && p!=NULL) { j++; p=p->next; } if (p==NULL) return false; else { q=p->next; if (q==NULL) return false; e=q->data; p->next=q->next; free(q); return true; } } int main() { LinkList *L; char e; cout<<"(1)初始化單鏈表h"<<endl; InitList(L); cout<<"(2)依次採用尾插法插入a,b,c,d,e元素"<<endl; ListInsert(L,1,'a'); ListInsert(L,2,'b'); ListInsert(L,3,'c'); ListInsert(L,4,'d'); ListInsert(L,5,'e'); cout<<"(3)輸出單鏈表h:"; DispList(L); cout<<"(4)單鏈表h長度="<<ListLength(L)<<endl; cout<<"(5)單鏈表h為"<<(ListEmpty(L)?"空":"非空")<<endl; GetElem(L,3,e); cout<<"(6)單鏈表h的第3個元素為"<<e<<endl;wew cout<<"(7)元素a的位置="<<LocateElem(L,'a')<<endl; cout<<"(8)在第4個元素位置上插入f元素"<<endl; ListInsert(L,4,'f'); cout<<"(9)輸出單鏈表h:"; DispList(L); cout<<"(10)刪除L的第3個元素"<<endl; ListDelete(L,3,e); cout<<"(11)輸出單鏈表h:"; DispList(L); cout<<"(12)釋放單鏈表h"<<endl; DestroyList(L); return 0; }
6.編寫一個程式,採用單鏈表表示集合(集合中不存在重複的元素),將其按遞增方式排序,構成有序單鏈表。並求這樣的兩個集合的並、交和差。
#include <iostream> #include <cstdio> #include <malloc.h> using namespace std; typedef struct LNode //定義單鏈表結點型別 { char data; struct LNode *next; } LinkList; void DispList(LinkList *L) { LinkList *p=L->next; while (p!=NULL) { printf("%c ",p->data); p=p->next; } printf("\n"); } void DestroyList(LinkList *&L) { LinkList *p=L,*q=p->next; while (q!=NULL) { free(p); p=q; q=p->next; } free(p); } void CreateListR(LinkList *&L,char a[],int n) //尾插法建表 { LinkList *s,*r;int i; L=(LinkList *)malloc(sizeof(LinkList)); //建立頭結點 L->next=NULL; r=L; //r始終指向終端結點,開始時指向頭結點 for (i=0;i<n;i++) { s=(LinkList *)malloc(sizeof(LinkList));//建立新結點 s->data=a[i]; r->next=s; //將*s插入*r之後 r=s; } r->next=NULL; //終端結點next域置為NULL } void sort(LinkList *&L) //單鏈表元素遞增排序 { LinkList *p,*pre,*q; p=L->next->next; //p指向L的第2個數據節點 L->next->next=NULL; //構造只含一個數據節點的有序表 while (p!=NULL) { q=p->next; //q儲存*p節點後繼節點的指標 pre=L; //從有序表開頭進行比較,pre指向插入*p的前驅節點 while (pre->next!=NULL && pre->next->data<p->data) pre=pre->next; //在有序表中找插入*p的前驅節點*pre p->next=pre->next; //將*pre之後插入*p pre->next=p; p=q; //掃描原單鏈表餘下的節點 } } void Union(LinkList *ha,LinkList *hb,LinkList *&hc) //求兩有序集合的並 { LinkList *pa=ha->next,*pb=hb->next,*s,*tc; hc=(LinkList *)malloc(sizeof(LinkList)); //建立頭結點 tc=hc; while (pa!=NULL && pb!=NULL) { if (pa->data<pb->data) { s=(LinkList *)malloc(sizeof(LinkList)); //複製結點 s->data=pa->data; tc->next=s;tc=s; pa=pa->next; } else if (pa->data>pb->data) { s=(LinkList *)malloc(sizeof(LinkList)); //複製結點 s->data=pb->data; tc->next=s;tc=s; pb=pb->next; } else { s=(LinkList *)malloc(sizeof(LinkList)); //複製結點 s->data=pa->data; tc->next=s;tc=s; pa=pa->next; //重複的元素只複製一個 pb=pb->next; } } if (pb!=NULL) pa=pb; //複製餘下的結點 while (pa!=NULL) { s=(LinkList *)malloc(sizeof(LinkList)); //複製結點 s->data=pa->data; tc->next=s;tc=s; pa=pa->next; } tc->next=NULL; } void InterSect(LinkList *ha,LinkList *hb,LinkList *&hc) //求兩有序集合的差 { LinkList *pa=ha->next,*pb,*s,*tc; hc=(LinkList *)malloc(sizeof(LinkList)); tc=hc; while (pa!=NULL) { pb=hb->next; while (pb!=NULL && pb->data<pa->data) pb=pb->next; if (pb!=NULL && pb->data==pa->data) //若pa結點值在B中 { s=(LinkList *)malloc(sizeof(LinkList)); //複製結點 s->data=pa->data; tc->next=s;tc=s; } pa=pa->next; } tc->next=NULL; } void Subs(LinkList *ha,LinkList *hb,LinkList *&hc) //求兩有序集合的差 { LinkList *pa=ha->next,*pb,*s,*tc; hc=(LinkList *)malloc(sizeof(LinkList)); tc=hc; while (pa!=NULL) { pb=hb->next; while (pb!=NULL && pb->data<pa->data) pb=pb->next; if (!(pb!=NULL && pb->data==pa->data)) //若pa結點值不在B中 { s=(LinkList *)malloc(sizeof(LinkList)); //複製結點 s->data=pa->data; tc->next=s;tc=s; } pa=pa->next; } tc->next=NULL; } int main() { LinkList *ha,*hb,*hc; char a[]={'d','a','e','b'}; char b[]={'f','e','b','h','c','a'}; cout<<"集合的運算如下:"<<endl; CreateListR(ha,a,4); CreateListR(hb,b,6); cout<<"原 集 合A: "; DispList(ha); cout<<"原 集 合B: "; DispList(hb); sort(ha); sort(hb); cout<<"有序集合A: "; DispList(ha); cout<<"有序集合B: "; DispList(hb); Union(ha,hb,hc); cout<<"集合的並集C: "; DispList(hc); InterSect(ha,hb,hc); cout<<"集合的交集C: "; DispList(hc); Subs(ha,hb,hc); cout<<"集合的差集C: "; DispList(hc); DestroyList(ha); DestroyList(hb); DestroyList(hc); }