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

提交评论