數據結構實驗3_第1頁
數據結構實驗3_第2頁
數據結構實驗3_第3頁
已閱讀5頁,還剩12頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、WORD格式"數據構造與算法"實驗報告實驗序號: 3實驗工程名稱:鏈式表的操作學號 1507112104姓名陳忠表專業、班15 商智實驗地點指導教師林開標實驗時間16.11.09一、實驗目的及要求1. 通過實驗理解單鏈表的邏輯構造;2. 通過實驗掌握單鏈表的根本操作和具體的函數實現。二、實驗設備環境及要求微型計算機;windows 操作系統;Microsoft Visual Studio 6.0 集成開發環境。三、實驗內容與步驟鏈式表表示和實現線性表的如下:#include"stdio.h"#include"stdlib.h"專業資料

2、整理WORD格式typedef struct node/定義結點專業資料整理WORD格式int data;/結點的數據域為整型struct node *next;/結點的指針域ListNode;typedef ListNode * LinkList;/自定義 LinkList單鏈表類型LinkList CreatListR1();/函數,用尾插入法建立帶頭結點的單鏈表ListNode *LocateNode(LinkList head, int key);/函數,按值查找結點voidDeleteList(LinkListhead,intkey);/函數,刪除指定值的結點專業資料整理WORD格式

3、void printlist(LinkList head);voidDeleteAll(LinkListhead);/函數,打印鏈表中的所有值函數,刪除所有結點,釋放內存專業資料整理WORD格式/= 主函數 =void main()int num;char ch;LinkList head;專業資料整理WORD格式head=CreatListR1(); printlist(head);/用尾插入法建立單鏈表,返回頭指針遍歷鏈表輸出其值專業資料整理WORD格式printf(" Delete node (y/n):"); /輸入 "y" 或 "n&

4、quot; 去選擇是否刪除結點scanf("%c",&ch);if(ch=y ) | ch=Y )printf("Please input Delete_data:");scanf("%d",num);/輸入要刪除的字符串DeleteList(head,num);printlist(head);DeleteAll(head);/刪除所有結點,釋放內存/= 用尾插入法建立帶頭結點的單鏈表=LinkList CreatListR1(void)return head;/返回頭指針/= 按值查找結點,找到那么返回該結點的位置,否那么返

5、回 NULL= ListNode *LocateNode(LinkList head, int key)return p;/假設p=NULL那么查找失敗,否那么p 指向找到的值為key 的結點/= 刪除帶頭結點的單鏈表中的指定結點=void DeleteList(LinkList head,int key)/ 按 key 值查找結點的/ 假設沒有找到結點,退出/ 假設找到,那么從單鏈表中刪除該結點,并釋放結點/= 打印鏈表 , 輸出所有結點的值=void printlist(LinkList head)/= 刪除所有結點,釋放空間=void DeleteAll(LinkList head)專業

6、資料整理WORD格式1、 實現并調試單鏈表的的相關算法;專業資料整理WORD格式2、改寫以上程序,實現功能如下:(1 編寫一個刪除鏈表中值為 x 的結點的直接前趨結點的算法,假設有多個值為 x 的結點,那么刪除第一個 x 的直接前趨結點。(2 改寫 CreatListR1函數,使得鏈表創立時為非遞減有序的單鏈表。(3 在算法 (2 生成的非遞減有序的單鏈表中,編寫一個算法,刪除單鏈表中值一樣的多余結點。(4 寫一個對單循環鏈表進展逆序輸出打印每個結點的值的算法。四、實驗結果與數據處理一 .實驗結果如圖1 所示:圖 1二 . 1實驗結果如圖2 所示:專業資料整理WORD格式圖 22實驗結果如圖3

7、 所示:圖 33實驗結果如圖4 所示:專業資料整理WORD格式圖 4(4) 實驗結果如圖5 所示:圖 5五、分析與討論感覺實驗3 比之前的實驗一、二難度更大,只能瀏覽同學的,有疑問便問同學,這樣勉強理解。專業資料整理WORD格式六、教師評語成績簽名:日期:附源程序清單:一 .#include"stdio.h"#include"stdlib.h"typedef struct node/ 定義結點int data;/ 結點的數據域為整型struct node *next;/ 結點的指針域ListNode;typedef ListNode * LinkList

8、;/ 自定義 LinkList 單鏈表類型LinkList CreatListR1();/ 函數,用尾插入法建立帶頭結點的單鏈表void printlist(LinkList head);/函數,打印鏈表中的所有值ListNode *LocateNode(LinkList head, int key);/ 函數,按值查找結點void DeleteList(LinkList head,int key);/ 函數,刪除指定值的結點void DeleteAll(LinkList head);void main()int num;char ch;LinkList head;head=CreatList

9、R1();printf("List:n");printlist(head);printf(" Delete node (y/n):");/輸入 "y" 或 "n" 去選擇是否刪除結點getchar();scanf("%c",&ch);if(ch='y'|ch='Y')printf("Please input Delete_data:");scanf("%d",&num);/輸入要刪除的數DeleteList(

10、head,num);/刪除printlist(head);/打印DeleteAll(head);/ 刪除所有結點,釋放內存專業資料整理WORD格式專業資料整理WORD格式/= 用尾插入法建立帶頭結點的單鏈表=LinkList CreatListR1(void)int n,i,count;LinkList head=(LinkList)malloc(sizeof(ListNode);ListNode *s, *r;/s 用來指向新生成的節點。r 始終指向 L 的終端節點。r=head;r->next=NULL;printf(" 請輸入鏈表節點數:");scanf(&qu

11、ot;%d",&n);printf(" 輸入節點值:");for ( i = 0; i < n; i+) s = (LinkList)malloc(sizeof(ListNode);/s 指向新申請的節點 scanf("%d",&count);s->data = count; / 用新節點的數據域來承受ir->next = s; / 用 r 來接納新節點專業資料整理WORD格式r = s; /r指向終端節點專業資料整理WORD格式r->next = NULL;專業資料整理WORD格式return head

12、;/返回頭指針return head;/返回頭指針專業資料整理WORD格式void printlist(LinkList head)專業資料整理WORD格式ListNode *p=head->next;while(p)printf("%d, ",p->data);/ 從開場結點打印專業資料整理WORD格式p=p->next;printf("n");/= 按值查找結點,找到那么返回該結點的位置,否那么返回NULL=ListNode *LocateNode(LinkList head, int key)專業資料整理WORD格式ListNod

13、e *p=head->next; / 從開場結點比較while(p && p->data!=key)/直到 p 為 NULLp=p->next;/掃描下一個結點return p;/ 假設 p=NULL那么查找失敗,否那么或 p->data 為 key 止p 指向找到的值為key 的結點專業資料整理WORD格式/= 刪除帶頭結點的單鏈表中的指定結點=void DeleteList(LinkList head,int key)ListNode *p,*r,*q=head;p=LocateNode(head,key);/按 key 值查找結點的if(p=NUL

14、L ) / 假設沒有找到結點,退出專業資料整理WORD格式printf("position error");exit(0);while(q->next!=p)/p 為要刪除的結點,q 為 p 的前結點q=q->next;r=q->next;q->next=r->next;free(r);/ 釋放結點/= 刪除所有結點,釋放空間=void DeleteAll(LinkList head)ListNode *p=head,*r;while(p->next)r=p->next;free(p);p=r;free(p);二 1專業資料整理WO

15、RD格式#include"stdio.h"#include"stdlib.h"typedef struct node/ 定義結點專業資料整理WORD格式int data;/ 結點的數據域為整型struct node *next;/ 結點的指針域ListNode;typedef ListNode * LinkList;/ 自定義 LinkList 單鏈表類型LinkList CreatListR1();/ 函數,用尾插入法建立帶頭結點的單鏈表void printlist(LinkList head);ListNode *LocateNode(LinkLis

16、t head, int key);/ 函數,按值查找前結點void DeleteList(LinkList head,int key);/函數,刪除指定值的結點void DeleteAll(LinkList head);void main()int num;專業資料整理WORD格式char ch;LinkList head;head=CreatListR1();printf("List:n");printlist(head);專業資料整理WORD格式printf(" 是否刪除鏈表中值為 x 的結點的直接前趨結點 (y/n):"); / 輸入 "

17、y" 或 "n" 去選擇是否刪除結點getchar();scanf("%c",&ch);if(ch='y'|ch='Y')printf("Please input Delete_data:");scanf("%d",&num);/輸入要刪除的字符串DeleteList(head,num);printlist(head);DeleteAll(head);/ 刪除所有結點,釋放內存/= 用尾插入法建立帶頭結點的單鏈表=LinkList CreatListR1(v

18、oid)int n,i,count;LinkList head=(LinkList)malloc(sizeof(ListNode);ListNode *s, *r;/s 用來指向新生成的節點。r 始終指向 L 的終端節點。r=head;r->next=NULL;printf(" 請輸入鏈表節點數:");scanf("%d",&n);printf(" 輸入節點值:");for ( i = 0; i < n; i+) s = (LinkList)malloc(sizeof(ListNode);/s 指向新申請的節點sc

19、anf("%d",&count);s->data = count; / 用新節點的數據域來承受ir->next = s; / 用 r 來接納新節點專業資料整理WORD格式r = s; /r指向終端節點專業資料整理WORD格式r->next = NULL;專業資料整理WORD格式return head;/返回頭指針return head;/返回頭指針專業資料整理WORD格式void printlist(LinkList head)專業資料整理WORD格式ListNode *p=head->next;while(p)printf("%d

20、, ",p->data);p=p->next;/ 從開場結點打印專業資料整理WORD格式printf("n");/=/按值查找結點,找到返回該結點的直接前驅結點位置,否那么返回NULL=ListNode *LocateNode(LinkList head, int key)ListNode *p=head->next;專業資料整理WORD格式ListNode *x=head->next;/ while(p && p->data!=key)/從開場結點比較直到 p 為 NULL或p->data為 key 止專業資料

21、整理WORD格式專業資料整理WORD格式x=p; / x 為 P 的前一個節點;p=p->next;/ 掃描下一個結點if( p->data!=key)x=NULL;專業資料整理WORD格式return x;/ 假設p=NULL那么查找失敗,否那么p 指向找到的值為key 的結點專業資料整理WORD格式/= 刪除帶頭結點的單鏈表中的指定結點=void DeleteList(LinkList head,int key)ListNode *p,*r,*q=head;p=LocateNode(head,key);/按 key 值查找結點的if(p=NULL ) / 假設沒有找到結點,退出

22、printf("position error");exit(0);while(q->next!=p)/p 為要刪除的結點,q 為 p 的前結點q=q->next;r=q->next;q->next=r->next;free(r);/ 釋放結點/= 刪除所有結點,釋放空間=void DeleteAll(LinkList head)ListNode *p=head,*r;while(p->next)r=p->next;free(p);p=r;專業資料整理WORD格式free(p); 2專業資料整理WORD格式#include"

23、stdio.h"#include"stdlib.h"typedef struct node/ 定義結點專業資料整理WORD格式int data;struct node *next;/ 結點的數據域為整型/ 結點的指針域專業資料整理WORD格式ListNode;專業資料整理WORD格式typedef ListNode * LinkList; LinkList CreatListR1();/ 自定義 LinkList 單鏈表類型/ 函數,用尾插入法建立帶頭結點的單鏈表專業資料整理WORD格式void printlist(LinkList head);專業資料整理WOR

24、D格式void DeleteAll(LinkList head);void main()int num;char ch;LinkList head;head=CreatListR1();printf("List:n");printlist(head);DeleteAll(head);/ 刪除所有結點,釋放內存/= 用尾插入法建立帶頭結點的單鏈表=LinkList CreatListR1(void)int n,i,count,change,j;LinkList head=(LinkList)malloc(sizeof(ListNode);ListNode *s, *r,*q;

25、/s用來指向新生成的節點。r 始終指向L 的終端節點。r=head;q=head;r->next=NULL;printf(" 請輸入鏈表節點數:");scanf("%d",&n);printf(" 輸入節點值:");for ( i = 0; i < n; i+) s = (LinkList)malloc(sizeof(ListNode);/s 指向新申請的節點scanf("%d",&count);s->data = count; / 用新節點的數據域來承受i專業資料整理WORD格式

26、r->next = s; / 用 r 來接納新節點r = s; /r 指向終端節點r->next = NULL;/ 排序;for(i=n;i>0;i-)q=head->next;for(j=0;j<n-1;j+)if(q->data)>(q->next->data)change=q->data;q->data=q->next->data;q->next->data=change;q=q->next;elseq=q->next;專業資料整理WORD格式return head;/返回頭指針retu

27、rn head;/返回頭指針專業資料整理WORD格式void printlist(LinkList head)專業資料整理WORD格式ListNode *p=head->next;while(p)printf("%d, ",p->data);p=p->next;/ 從開場結點打印專業資料整理WORD格式printf("n");專業資料整理WORD格式/= 刪除所有結點,釋放空間=void DeleteAll(LinkList head)ListNode *p=head,*r;while(p->next)r=p->next;f

28、ree(p);p=r;專業資料整理WORD格式free(p); 3專業資料整理WORD格式#include"stdio.h"#include"stdlib.h"typedef struct node/ 定義結點專業資料整理WORD格式int data;struct node *next;/ 結點的數據域為整型/ 結點的指針域專業資料整理WORD格式ListNode;專業資料整理WORD格式typedef ListNode * LinkList; LinkList CreatListR1();/ 自定義 LinkList 單鏈表類型/ 函數,用尾插入法建立

29、帶頭結點的單鏈表專業資料整理WORD格式void printlist(LinkList head);專業資料整理WORD格式void DeleteSameNode(LinkList head);void DeleteAll(LinkList head);void main()int num;char ch;LinkList head;head=CreatListR1();printf("List:n");printlist(head);DeleteSameNode(head);printlist(head);DeleteAll(head);/ 刪除所有結點,釋放內存/= 用

30、尾插入法建立帶頭結點的單鏈表=LinkList CreatListR1(void)int n,i,count,change,j;LinkList head=(LinkList)malloc(sizeof(ListNode);ListNode *s,*r,*q;/s用來指向新生成的節點。r 始終指向L 的終端節點。r=head;q=head;r->next=NULL;printf(" 請輸入鏈表節點數:");scanf("%d",&n);printf(" 輸入節點值:");for ( i = 0; i < n; i+

31、) s = (LinkList)malloc(sizeof(ListNode);/s 指向新申請的節點專業資料整理WORD格式scanf("%d",&count);s->data = count; / 用新節點的數據域來承受ir->next = s; / 用 r 來接納新節點r = s; /r 指向終端節點r->next = NULL;/ 排序;for(i=n;i>0;i-)q=head->next;for(j=0;j<n-1;j+)if(q->data)>(q->next->data)change=q-&

32、gt;data;q->data=q->next->data;q->next->data=change;q=q->next;elseq=q->next;專業資料整理WORD格式return head;/返回頭指針return head;/返回頭指針專業資料整理WORD格式void printlist(LinkList head)專業資料整理WORD格式ListNode *p=head->next;while(p)printf("%d, ",p->data);p=p->next;/ 從開場結點打印專業資料整理WORD格

33、式printf("n");專業資料整理WORD格式/= 刪除多余節點=void DeleteSameNode(LinkList head)int n=2;ListNode *p,*q,*t,*s;專業資料整理WORD格式p = head;p = p->next;/ p 第一個while(p->next)if(p->data = p->next->data)if(p->next->next=NULL)p->next=NULL;elsep->next=p->next->next;p=p->next;else

34、p=p->next;/= 刪除所有結點,釋放空間=void DeleteAll(LinkList head)ListNode *p=head,*r;while(p->next)r=p->next;free(p);p=r;free(p);專業資料整理WORD格式(4)#include"stdio.h"#include"stdlib.h"typedef struct node/ 定義結點專業資料整理WORD格式int data;struct node *next;ListNode;/ 結點的數據域為整型/ 結點的指針域專業資料整理WORD格

35、式typedef ListNode * LinkList; LinkList CreatListR1();/ 自定義 LinkList 單鏈表類型/ 函數,用尾插入法建立帶頭結點的單鏈表專業資料整理WORD格式void printlist(LinkList head);專業資料整理WORD格式void printlist_inverseorder(LinkList head);/逆序void DeleteAll(LinkList head);void main()int num;char ch;LinkList head;head=CreatListR1();printf("List:n");printlist(head);printlist_inverseorder(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論