c语言链表及其相关操作代码_第1页
c语言链表及其相关操作代码_第2页
c语言链表及其相关操作代码_第3页
c语言链表及其相关操作代码_第4页
c语言链表及其相关操作代码_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、链表动态创建#include<stdio.h>#include<string.h>#include<stdlib.h>struct 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

2、=(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=(struct Student *)malloc(sizeof(struct Student);printf("请输入学号姓名和成绩n&q

3、uot;);scanf("%s%s%lf",p1->num,p1->name,&p1->score);if(head!=NULL)p2->next=NULL;return head;void print_linklist(struct Student *head)struct Student *pi;if(head=NULL)printf("空链表!n");elseprintf("学号t姓名t成绩n");for(pi=head;pi!=NULL;pi=pi->next)printf("%

4、st%st%lfn",pi->num,pi->name,pi->score);int main()struct Student *head;head=creatlinklist();print_linklist(head);return 0;/链表查找#include<stdio.h>#include<string.h>#include<stdlib.h>struct Studentchar num10; /字符串学号char name10; /字符串姓名double score; /双精度实型成绩struct Student *

5、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=

6、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

7、=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);struct Student * find_linklist_by_name(struct Student *head,char name)struct Student *pi;struct Student *pr;pr=NULL;for(pi=head;pi!

8、=NULL;pi=pi->next)if(strcmp(name,pi->name)=0)pr=pi;break;return pr;void print_one_node(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

9、name10;head=creatlinklist();printlinklist(head);printf("请输入要查找学生的姓名n");scanf("%s",&name);pr=find_linklist_by_name(head,name);print_one_node(pr);/链表排序#include<stdio.h>#include<string.h>#include<stdlib.h>struct Studentchar num10; /字符串学号char name10; /字符串姓名doubl

10、e 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);w

11、hile(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

12、 *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);void select_sortlinklist_by_score(struct Student *head)struct Student *select_sortlinklist_by_scor

13、e_(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<pmin->java)pmin0=pi;pmin=pi->next;if(phead=NULL)phead=pmin;ptail=pmin;elseptail->

14、next=pmin;ptail=pmin;if(pmin=head)head=head->next;elsepmin0->next=pmin->next;if(phead!=NULL)ptail->next=NULL;return phead;int main()struct Student *head;head=creatlinklist();printlinklist(head);head=select_sortlinklist_by_score_(head);printf("按分数排序后n");printlinklist(head);retur

15、n 0;/链表按顺序插入#include<stdio.h>#include<string.h>#include<stdlib.h>struct 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 St

16、udent *)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=(struct Student *)malloc(sizeof(struct Student);printf("请输入学号姓名和成绩n");scanf

17、("%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%lfn&qu

18、ot;,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

19、Student *find_linklist_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

20、=head;for(pi=head;pi->next!=NULL;pi=pi->next)if(pi->next->score<pmin->score)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 ph

21、ead;struct Student * insertlinklist_by_score(struct Student *head,struct Student *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-&

22、gt;score>ptail->score)ptail->next=p;p->next=NULL;elseif(p->score<head->score)pt=head;head=p;p->next=pt;elsefor(pi=head;pi->next!=NULL;pi=pi->next)if(pi->next->score>p->score)pfirstmax=pi->next;pfirstmax0=pi;break;pfirstmax0->next=p;p->next=pfirstmax

23、;return head;int main()struct Student *head;struct Student *p1;head=creatlinklist();printlinklist(head);p1=(struct Student *)malloc(sizeof(struct Student);printf("请输入学号姓名和成绩n");scanf("%s%s%lf",p1->num,p1->name,&p1->score);p1->next=NULL;head=insertlinklist_by_score

24、(head,p1);printlinklist(head);return 0;/合并两个链表并排序#include<stdio.h>#include<string.h>#include<stdlib.h>struct Studentchar name20; /字符型学生姓名double java; /双精度实型Java成绩struct Student *next; /用与构建连表指向下一结点;struct Student *creatlinklist()struct Student*head,*p1,*p2;int n;n=0;head=NULL;p1=(s

25、truct 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(&quo

26、t;%s%lf",p1->name,&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-

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论