c语言链表及其相关操作代码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

链表动态创建#includestdio.h#includestring.h#includestdlib.hstructStudent{charnum[10];//字符串学号charname[10];//字符串姓名doublescore;//双精度实型成绩structStudent*next;//用与构建链表指向下一结点};structStudent*creatlinklist()//当成绩项目输入0时,创建链表结束{structStudent*head;structStudent*p1,*p2;intn;n=0;head=NULL;p1=(structStudent*)malloc(sizeof(structStudent));printf(请输入学号姓名和成绩\n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);while(p1-score!=0){if(n==0){head=p1;p2=p1;n++;}else{p2-next=p1;p2=p1;n++;}p1=(structStudent*)malloc(sizeof(structStudent));printf(请输入学号姓名和成绩\n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);}if(head!=NULL)p2-next=NULL;returnhead;}voidprint_linklist(structStudent*head){structStudent*pi;if(head==NULL){printf(空链表!\n);}else{printf(学号\t姓名\t成绩\n);for(pi=head;pi!=NULL;pi=pi-next){printf(%s\t%s\t%lf\n,pi-num,pi-name,pi-score);}}}intmain(){structStudent*head;head=creatlinklist();print_linklist(head);return0;}/////////////////////////////////////////////////////////////////链表查找#includestdio.h#includestring.h#includestdlib.hstructStudent{charnum[10];//字符串学号charname[10];//字符串姓名doublescore;//双精度实型成绩structStudent*next;//用与构建链表指向下一结点};structStudent*creatlinklist(){structStudent*head;structStudent*p1,*p2;intn;n=0;head=NULL;p1=(structStudent*)malloc(sizeof(structStudent));printf(请输入学号姓名和成绩\n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);while(p1-score!=0){if(n==0){head=p1;p2=p1;n++;}else{p2-next=p1;p2=p1;n++;}p1=(structStudent*)malloc(sizeof(structStudent));printf(请输入学号姓名和成绩\n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);}if(head!=NULL)p2-next=NULL;returnhead;}voidprintlinklist(structStudent*head){structStudent*pi;if(head==NULL){printf(空链表!\n);}else{printf(学号\t姓名\t成绩\n);for(pi=head;pi!=NULL;pi=pi-next){printf(%s\t%s\t%lf\n,pi-num,pi-name,pi-score);}}}structStudent*find_linklist_by_name(structStudent*head,charname[]){structStudent*pi;structStudent*pr;pr=NULL;for(pi=head;pi!=NULL;pi=pi-next){if(strcmp(name,pi-name)==0){pr=pi;break;}}returnpr;}voidprint_one_node(structStudent*p){if(p==NULL){printf(查无此人\n);}else{printf(学号\t姓名\t成绩\n);printf(%s\t%s\t%lf\n,p-num,p-name,p-score);}}intmain(){structStudent*head;structStudent*pr;charname[10];head=creatlinklist();printlinklist(head);printf(请输入要查找学生的姓名\n);scanf(%s,&name);pr=find_linklist_by_name(head,name);print_one_node(pr);}/////////////////////////////////////////////////链表排序#includestdio.h#includestring.h#includestdlib.hstructStudent{charnum[10];//字符串学号charname[10];//字符串姓名doublescore;//双精度实型成绩structStudent*next;//用与构建链表指向下一结点};structStudent*creatlinklist(){structStudent*head;structStudent*p1,*p2;intn;n=0;head=NULL;p1=(structStudent*)malloc(sizeof(structStudent));printf(请输入学号姓名和成绩\n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);while(p1-score!=0){if(n==0){head=p1;p2=p1;n++;}else{p2-next=p1;p2=p1;n++;}p1=(structStudent*)malloc(sizeof(structStudent));printf(请输入学号姓名和成绩\n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);}if(head!=NULL)p2-next=NULL;returnhead;}voidprintlinklist(structStudent*head){structStudent*pi;if(head==NULL){printf(空链表!\n);}else{printf(学号\t姓名\t成绩\n);for(pi=head;pi!=NULL;pi=pi-next){printf(%s\t%s\t%lf\n,pi-num,pi-name,pi-score);}}}voidselect_sortlinklist_by_score(structStudent*head){}structStudent*select_sortlinklist_by_score_(structStudent*head){structStudent*pi;structStudent*phead,*ptail;structStudent*pmin,*pmin0;phead=NULL;while(head!=NULL){pmin=head;for(pi=head;pi-next!=NULL;pi=pi-next){if(pi-next-javapmin-java){pmin0=pi;pmin=pi-next;}}if(phead==NULL){phead=pmin;ptail=pmin;}else{ptail-next=pmin;ptail=pmin;}if(pmin==head){head=head-next;}else{pmin0-next=pmin-next;}}if(phead!=NULL)ptail-next=NULL;returnphead;}intmain(){structStudent*head;head=creatlinklist();printlinklist(head);head=select_sortlinklist_by_score_(head);printf(按分数排序后\n);printlinklist(head);return0;}///////////////////////////////////////链表按顺序插入#includestdio.h#includestring.h#includestdlib.hstructStudent{charnum[10];//字符串学号charname[10];//字符串姓名doublescore;//双精度实型成绩structStudent*next;//用与构建链表指向下一结点};structStudent*creatlinklist(){structStudent*head;structStudent*p1,*p2;intn;n=0;head=NULL;p1=(structStudent*)malloc(sizeof(structStudent));printf(请输入学号姓名和成绩\n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);while(p1-score!=0){if(n==0){head=p1;p2=p1;n++;}else{p2-next=p1;p2=p1;n++;}p1=(structStudent*)malloc(sizeof(structStudent));printf(请输入学号姓名和成绩\n);scanf(%s%s%lf,p1-num,p1-name,&p1-score);}if(head!=NULL)p2-next=NULL;returnhead;}voidprintlinklist(structStudent*head){structStudent*pi;if(head==NULL){printf(空链表!\n);}else{printf(学号\t姓名\t成绩\n);for(pi=head;pi!=NULL;pi=pi-next){printf(%s\t%s\t%lf\n,pi-num,pi-name,pi-score);}}}structStudent*find_linklist_by_name(structStudent*head,charname[]){structStudent*pi;structStudent*pr;pr=NULL;for(pi=head;pi!=NULL;pi=pi-next){if(strcmp(name,pi-name)==0){pr=pi;break;}}returnpr;}voidsortlinklist_by_score(structStudent*head){}structStudent*find_linklist_tail(structStudent*head){structStudent*pi;pi=head;while(pi-next!=NULL){pi=pi-next;}returnpi;}struct

1 / 14
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功