軟基上機報告_第1頁
軟基上機報告_第2頁
軟基上機報告_第3頁
已閱讀5頁,還剩9頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

1、一、程序流程說明二、1)創建一個單鏈表,其數據元素為整數,從鍵盤輸入,輸入0 結束(注意 0 不放到鏈表內);三、2)從鍵盤任意輸入一個整數, 在單鏈表中查詢該數, 如果單鏈表中已經存在這個數,就調用刪除函數, 刪除該元素所在結點, 并將單鏈表在刪除前后的數據元素依次輸出到屏幕 上;四、如果單鏈表中不存在這個數,就調用插入函數,將這個數插入到單鏈表尾,并將單 鏈表在插入前后的數據元素依次輸出到屏幕上。五、3)教材第一章習題第 9題(用鏈表實現)ex2_3擴展題六、1)刪除單鏈表中全部的負數七、2)創建一個雙向鏈表, 按照冒泡排序的思路對這個雙向鏈表進行排序, 打印排序結果。 注意,本算法在交換

2、元素時是將鏈點整個交換而不是將鏈點中的元素值交換。八、九、十、2_21)創建一個單鏈表, 其數據元素為整數, 從鍵盤輸入, 輸入 0結束(注意 0 不放到鏈表內) ; ( if x ! =0 , scanf)2)從鍵盤任意輸入一個整數,在單鏈表中查詢該數,如果單鏈表中已經存在這個數,就 調用刪除函數, 刪除該元素所在結點, 并將單鏈表在刪除前后的數據元素依次輸出到屏幕上;( search 返回重復的位置,然后刪除(同實驗一) ) 如果單鏈表中不存在這個數,就調用插入函數,將這個數插入到單鏈表尾,并將單鏈表 在插入前后的數據元素依次輸出到屏幕上。 (同實驗一,直接加在鏈表末尾)3)判斷插入元素與

3、表內元素的大小,如temp->data<x<temp->link->data ,則插入 temp 后2_31)刪除單鏈表中全部的負數(逐個判斷是不是負數,并且返回負數所在位置,刪除過春哥 同實驗一)2)創建一個雙向鏈表,按照冒泡排序的思路對這個雙向鏈表進行排序,打印排序結果。注意,本算法在交換元素時是將鏈點整個交換而不是將鏈點中的元素值交換。(從一開始逐個比較,將比第一位大的與第一位交換位置, 一直比較到最后一位然后開始對于第二位重復比 較)二、程序代碼2_2#include<stdio.h> #include<malloc.h> #inc

4、lude<stdlib.h> typedef struct Node int data; struct Node *link;node;typedef struct Listnode *head;node *tail;int length;list;void creat_list(list *table)int x,i;node *temp;table->head=NULL;table->tail=NULL;table->length=0;x=1;scanf("%d",&x);for(i=0;x!=0;i+)fflush(stdin);

5、printf("n");temp=(node*)malloc(sizeof(node); temp->data=x; temp->link=NULL; temp->link=table->head; table->head=temp; table->length+;scanf("%d",&x);node * create_node(int new_node)node * temp;temp = (node*)malloc(sizeof(node);temp->data = new_node;temp-&g

6、t;link = NULL;return temp;void get(list *table,int n)int i,loc=table->length+1;node *temp;node *newnode;temp=table->head;table->length+;newnode=create_node(n);for(i=2;i<loc;i+)temp=temp->link;newnode->link = temp->link; temp->link = newnode;void show_list(list *table)int x,i;

7、node *temp; printf(" 鏈表為: n"); temp=table->head;for(i=1;i <= table->length;i+) x=temp->data;printf("%d ",x);temp=temp->link;void delete_node(list *table,int n)int i;node *temp,*t;temp=table->head;if(n=1)temp=temp->link;table->head=temp;if(n>2&&n

8、<table->length)temp=table->head;for(i=2;i<n;i+)temp=temp->link; t=temp->link; temp->link=t->link;if(n=table->length) for(i=2;i<n;i+)temp=temp->link;temp->link=NULL;if(n=2)temp=table->head; t=temp->link; temp->link=t->link;table->length-;void search(

9、list *table,int x,int a2)int i=0,j=0;node *temp;temp=table->head;for(i=1;i<=table->length;i+) if(x=temp->data)a1=i;j+;temp=temp->link;if(j=0)a0=0;a1=0;elsea0=1;void main()list table;int m,a2;creat_list(&table); show_list(&table);printf("n 輸入一個整數 n"); scanf("%d&q

10、uot;,&m); search(&table,m,a);if(a0=1)delete_node(&table,a1);show_list(&table);else get(&table,m); show_list(&table);2_2(3) #include<stdio.h> #include<malloc.h> #include<stdlib.h> typedef struct Nodeint data;struct Node *link;node; typedef struct Listnode *he

11、ad;node *tail;int length;list;void creat_list(list *table)int x,i;node *temp;table->head=NULL; table->tail=NULL;table->length=0;x=1;scanf("%d",&x);for(i=0;x!=0;i+)fflush(stdin); printf("n"); temp=(node*)malloc(sizeof(node); temp->data=x;temp->link=NULL; temp-&g

12、t;link=table->head; table->head=temp; table->length+;scanf("%d",&x);node * create_node(int new_node)node * temp;temp = (node*)malloc(sizeof(node); temp->data = new_node;temp->link = NULL;return temp;void get(list *table,int n)int i,m=0;node *temp;node *newnode;temp=table

13、->head;table->length+;newnode=create_node(n); if(n<temp->data)newnode->link=temp;temp=newnode;elsefor(i=1;m=0;i+) if(i<=table->length&&temp->data<n&&n<=temp->link->data) newnode->link = temp->link;temp->link = newnode;m=1;else if(i=table-&

14、gt;length) temp->link=newnode; newnode->link=NULL; m=1; temp=temp->link;table->length+;void show_list(list *table)int x,i;node *temp;printf(" 鏈表為: n"); temp=table->head;for(i=1;i <= table->length;i+) x=temp->data;printf("%d ",x); temp=temp->link;void ma

15、in()list table;int m,a2;creat_list(&table); show_list(&table);printf("n 輸入一個整數 n"); scanf("%d",&m);get(&table,m); show_list(&table);2_3(1)#include<stdio.h> #include<malloc.h> #include<stdlib.h> typedef struct Nodeint data;struct Node *link;no

16、de;typedef struct List node *head; node *tail; int length;list;void creat_list(list *table) int x,i; node *temp; table->head=NULL; table->tail=NULL; table->length=0; x=1; scanf("%d",&x); for(i=0;x!=0;i+) fflush(stdin);printf("n");temp=(node*)malloc(sizeof(node); temp

17、->data=x;temp->link=NULL; temp->link=table->head; table->head=temp; table->length+; scanf("%d",&x);node * create_node(int new_node)node * temp;temp = (node*)malloc(sizeof(node);temp->data = new_node;temp->link = NULL;return temp;void get(list *table,int n)int i,

18、loc=table->length+1;node *temp;node *newnode;temp=table->head;table->length+;newnode=create_node(n);for(i=2;i<loc;i+)temp=temp->link;newnode->link = temp->link; temp->link = newnode;void show_list(list *table)int x,i;node *temp; printf("n 鏈表為: n"); temp=table->he

19、ad;for(i=1;i <= table->length;i+) x=temp->data;printf("%d ",x);temp=temp->link;void delete_node(list *table,int n)int i;node *temp,*t;temp=table->head;if(n=1)temp=temp->link;table->head=temp;if(n>2&&n<table->length)temp=table->head;for(i=2;i<n;i+

20、)temp=temp->link; t=temp->link; temp->link=t->link;if(n=table->length) for(i=2;i<n;i+)temp=temp->link;temp->link=NULL;if(n=2)temp=table->head; t=temp->link; temp->link=t->link;table->length-;void search(list *table,int a2)int i=0,j=0;node *temp;temp=table->h

21、ead;for(i=1;i<=table->length;i+) if(temp->data<0)a1=i;j=1;break;temp=temp->link;if(j=0)a0=0;a1=0;else a0=1;void paixu(list *table)int i,j,m,u;node *temp,*t,*p;for(i=1;i<=table->length;i+)temp=table->head;for(u=1;u<i;u+)temp=temp->link;t=temp; for(j=i;j<=table->len

22、gth;j+)temp=t;for(m=i;m<=j;m+) temp=temp->link;if(t->data<(temp->data)p=temp->link; temp->link=t->link; t->link=p;void main()list table;int m,a2;creat_list(&table);show_list(&table);search(&table,a);for(m=1;a0=1;m+)search(&table,a);if(a0=1)delete_node(&

23、table,a1);show_list(&table);2_3(2)#include<stdio.h>#include<malloc.h>#include<stdlib.h>typedef struct Nodeint data;struct Node *link,*last;node;typedef struct Listnode *head;node *tail;int length;list;void creat_list(list *table)int x,i;node *temp,*t,*p; table->head=NULL;tab

24、le->tail=NULL;table->length=0;x=1;scanf("%d",&x);for(i=0;x!=0;i+)fflush(stdin);printf("n");temp=(node*)malloc(sizeof(node); temp->data=x; temp->link=NULL; temp->link=table->head; table->head=temp; table->length+;scanf("%d",&x);t=(node*)ma

25、lloc(sizeof(node);t=table->head;t->last=NULL;for(i=2;i<=table->length;i+)p=t; t=t->link;t->last=p;void show_list(list *table)int x,i;node *temp; printf(" 鏈表為: n"); temp=table->head;for(i=1;i <= table->length;i+) x=temp->data; printf("%d ",x); temp=te

26、mp->link;void paixu(list *table)int i,j,m;node *temp,*t,*p,*q; temp=table->head; t=temp; for(i=2;i<=table->length;i+)t=t->link;if(t->data>temp->data) p=t->link; t->link=temp->link; temp->link=p; t->last->link=temp; table->head=t; t->last=NULL; temp->last=t->last; p=t; t=temp; temp=t;/*/temp=table->head; for(i=2;i<=table->length;i+) temp=temp->link;t=temp;for(j=i+1;j<=table->length;j+) t=t->link; if(t->data>temp->data) p=t->link; t->link=temp->link; temp->li

溫馨提示

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

評論

0/150

提交評論