c語言鏈表及其相關操作代碼_第1頁
c語言鏈表及其相關操作代碼_第2頁
c語言鏈表及其相關操作代碼_第3頁
c語言鏈表及其相關操作代碼_第4頁
c語言鏈表及其相關操作代碼_第5頁
已閱讀5頁,還剩9頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

1、鏈表動態創建#include#include#includestruct Studentchar num10; /字符串學號char name10; /字符串姓名double score; /雙精度實型成績struct Student *next; /用與構建鏈表指向下一結點;struct Student *creatlinklist()/當成績項目輸入0時,創建鏈表結束struct Student *head;struct Student *p1,*p2;int n;n=0;head=NULL;p1=(struct Student *)malloc(sizeof(struct Student

2、);printf(請輸入學號姓名和成績n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);while(p1-score!=0)if(n=0)head=p1;p2=p1;n+;elsep2-next=p1;p2=p1;n+;p1=(struct Student *)malloc(sizeof(struct Student);printf(請輸入學號姓名和成績n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);if(head!=NULL)p2-next=NULL;return head;void print_linklist(st

3、ruct Student *head)struct Student *pi;if(head=NULL)printf(空鏈表!n);elseprintf(學號t姓名t成績n);for(pi=head;pi!=NULL;pi=pi-next)printf(%st%st%lfn,pi-num,pi-name,pi-score);int main()struct Student *head;head=creatlinklist();print_linklist(head);return 0;/鏈表查找#include#include#includestruct Studentchar num10; /

4、字符串學號char name10; /字符串姓名double score; /雙精度實型成績struct Student *next; /用與構建鏈表指向下一結點;struct Student *creatlinklist()struct Student *head;struct Student *p1,*p2;int n;n=0;head=NULL;p1=(struct Student *)malloc(sizeof(struct Student);printf(請輸入學號姓名和成績n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);while(p1-sco

5、re!=0)if(n=0)head=p1;p2=p1;n+;elsep2-next=p1;p2=p1;n+;p1=(struct Student *)malloc(sizeof(struct Student);printf(請輸入學號姓名和成績n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);if(head!=NULL)p2-next=NULL;return head;void printlinklist(struct Student *head)struct Student *pi;if(head=NULL)printf(空鏈表!n);elseprintf

6、(學號t姓名t成績n);for(pi=head;pi!=NULL;pi=pi-next)printf(%st%st%lfn,pi-num,pi-name,pi-score);struct Student * find_linklist_by_name(struct Student *head,char name)struct Student *pi;struct Student *pr;pr=NULL;for(pi=head;pi!=NULL;pi=pi-next)if(strcmp(name,pi-name)=0)pr=pi;break;return pr;void print_one_no

7、de(struct Student *p)if(p=NULL)printf(查無此人n);elseprintf(學號t姓名t成績n);printf(%st%st%lfn,p-num,p-name,p-score);int main()struct Student *head;struct Student *pr;char name10;head=creatlinklist();printlinklist(head);printf(請輸入要查找學生的姓名n);scanf(%s,&name);pr=find_linklist_by_name(head,name);print_one_node(pr

8、);/鏈表排序#include#include#includestruct Studentchar num10; /字符串學號char name10; /字符串姓名double score; /雙精度實型成績struct Student *next; /用與構建鏈表指向下一結點;struct Student *creatlinklist()struct Student *head;struct Student *p1,*p2;int n;n=0;head=NULL;p1=(struct Student *)malloc(sizeof(struct Student);printf(請輸入學號姓名

9、和成績n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);while(p1-score!=0)if(n=0)head=p1;p2=p1;n+;elsep2-next=p1;p2=p1;n+;p1=(struct Student *)malloc(sizeof(struct Student);printf(請輸入學號姓名和成績n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);if(head!=NULL)p2-next=NULL;return head;void printlinklist(struct Student *hea

10、d)struct Student *pi;if(head=NULL)printf(空鏈表!n);elseprintf(學號t姓名t成績n);for(pi=head;pi!=NULL;pi=pi-next)printf(%st%st%lfn,pi-num,pi-name,pi-score);void select_sortlinklist_by_score(struct Student *head)struct Student *select_sortlinklist_by_score_(struct Student *head)struct Student *pi;struct Student

11、 *phead,*ptail;struct Student *pmin,*pmin0;phead=NULL;while(head!=NULL)pmin=head;for(pi=head;pi-next!=NULL;pi=pi-next)if(pi-next-javajava)pmin0=pi;pmin=pi-next;if(phead=NULL)phead=pmin;ptail=pmin;elseptail-next=pmin;ptail=pmin;if(pmin=head)head=head-next;elsepmin0-next=pmin-next;if(phead!=NULL)ptail

12、-next=NULL;return phead;int main()struct Student *head;head=creatlinklist();printlinklist(head);head=select_sortlinklist_by_score_(head);printf(按分數排序后n);printlinklist(head);return 0;/鏈表按順序插入#include#include#includestruct Studentchar num10; /字符串學號char name10; /字符串姓名double score; /雙精度實型成績struct Studen

13、t *next; /用與構建鏈表指向下一結點;struct Student *creatlinklist()struct Student *head;struct Student *p1,*p2;int n;n=0;head=NULL;p1=(struct Student *)malloc(sizeof(struct Student);printf(請輸入學號姓名和成績n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);while(p1-score!=0)if(n=0)head=p1;p2=p1;n+;elsep2-next=p1;p2=p1;n+;p1=(s

14、truct Student *)malloc(sizeof(struct Student);printf(請輸入學號姓名和成績n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);if(head!=NULL)p2-next=NULL;return head;void printlinklist(struct Student *head)struct Student *pi;if(head=NULL)printf(空鏈表!n);elseprintf(學號t姓名t成績n);for(pi=head;pi!=NULL;pi=pi-next)printf(%st%st%l

15、fn,pi-num,pi-name,pi-score);struct Student * find_linklist_by_name(struct Student *head,char name)struct Student *pi;struct Student *pr;pr=NULL;for(pi=head;pi!=NULL;pi=pi-next)if(strcmp(name,pi-name)=0)pr=pi;break;return pr;void sortlinklist_by_score(struct Student *head)struct Student *find_linklis

16、t_tail(struct Student *head)struct Student *pi;pi=head;while(pi-next!=NULL)pi=pi-next;return pi;struct Student *select_sortlinklist_by_score_(struct Student *head)struct Student *pi;struct Student *phead,*ptail;struct Student *pmin,*pmin0;phead=NULL;while(head!=NULL)pmin=head;for(pi=head;pi-next!=NU

17、LL;pi=pi-next)if(pi-next-scorescore)pmin0=pi;pmin=pi-next;if(phead=NULL)phead=pmin;ptail=pmin;elseptail-next=pmin;ptail=pmin;if(pmin=head)head=head-next;elsepmin0-next=pmin-next;if(phead!=NULL)ptail-next=NULL;return phead;struct Student * insertlinklist_by_score(struct Student *head,struct Student *

18、p)struct Student *pi;struct Student *pfirstmax,*pfirstmax0;struct Student *ptail;struct Student *pt;head=select_sortlinklist_by_score_(head);if(head=NULL)head=p;p-next=NULL;elseptail=find_linklist_tail(head);if(p-scoreptail-score)ptail-next=p;p-next=NULL;elseif(p-scorescore)pt=head;head=p;p-next=pt;

19、elsefor(pi=head;pi-next!=NULL;pi=pi-next)if(pi-next-scorep-score)pfirstmax=pi-next;pfirstmax0=pi;break;pfirstmax0-next=p;p-next=pfirstmax;return head;int main()struct Student *head;struct Student *p1;head=creatlinklist();printlinklist(head);p1=(struct Student *)malloc(sizeof(struct Student);printf(請

20、輸入學號姓名和成績n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);p1-next=NULL;head=insertlinklist_by_score(head,p1);printlinklist(head);return 0;/合并兩個鏈表并排序#include#include#includestruct Studentchar name20; /字符型學生姓名double java; /雙精度實型Java成績struct Student *next; /用與構建連表指向下一結點;struct Student *creatlinklist()struct

21、Student*head,*p1,*p2;int n;n=0;head=NULL;p1=(struct Student*)malloc(sizeof(struct Student);printf(請輸入姓名和java成績:);scanf(%s%lf,p1-name,&p1-java);while(p1-java!=0)if(n=0)head=p1;p2=p1;n+;elsep2-next=p1;p2=p1;n+;p1=(struct Student*)malloc(sizeof(struct Student);printf(請輸入姓名和java成績:);scanf(%s%lf,p1-name,

22、&p1-java);if(head!=NULL)p2-next=NULL;return head;void printlinklist(struct Student *head)struct Student *p;if(head=NULL)printf(姓名tjava成績n);elseprintf(姓名tjava成績n);for(p=head;p!=NULL;p=p-next)printf(%st%lfn,p-name,p-java);struct Student *select_sortlinklist_by_java(struct Student *head)struct Student *pi;struct Student *phead,*ptail;struct Student *pmin,*pmin0;phead=NULL;while(head!=NULL)pmin=head;for(pi=head;pi-next!=NULL;pi=pi-next)if(pi-next-java

溫馨提示

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

評論

0/150

提交評論