资源描述:
《科大c语言链表文件大作业.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#include#defineSTUDENTstructstudentdoubleaver,max;structstudent{intnum;floatscore;structstudent*next;};STUDENT*make(intn,floats){STUDENT*p;p=(STUDENT*)malloc(sizeof(STUDENT));p->num=n;p->score=s;p->next=NULL;retu
2、rnp;}STUDENT*create1(void){STUDENT*head=NULL,*p,*q=NULL;intn;floats;while(1){printf("请输入学生的学号和成绩:");scanf("%d%f",&n,&s);if(n==0)break;p=make(n,s);if(head==NULL)head=p;elseq->next=p;q=p;}if(q)q->next=NULL;returnhead;}STUDENT*create2(void){STUDENT*head=NULL,*p
3、;intn;floats;while(1){printf("请输入学生的学号和成绩:");scanf("%d%f",&n,&s);if(n==0)break;p=make(n,s);p->next=head;head=p;}returnhead;}STUDENT*insert1(STUDENT*head,STUDENT*p){STUDENT*q;intn=p->num;if(head==NULL
4、
5、head->num>n){p->next=head;head=p;return(head);}for(q=head
6、;q->next&&q->next->numnext);p->next=q->next;q->next=p;return(head);}STUDENT*create3(void){STUDENT*head=NULL,*p;intn;floats;while(1){printf("请输入学生的学号和成绩:");scanf("%d%f",&n,&s);if(n==0)break;p=make(n,s);head=insert1(head,p);}return(head);}STUDENT*inser
7、t2(intn,STUDENT*p,STUDENT*head){STUDENT*q;inti;if(n==1){p->next=head;head=p;returnhead;}for(q=head,i=1;q&&inext,i++);if(q){p->next=q->next;q->next=p;returnhead;}elseprintf("插入点位置过大,请修改插入位置:");}STUDENT*delect1(intn,STUDENT*head){STUDENT*p,*q;inti;if
8、(head==NULL)returnhead;if(n==1){p=head;head=head->next;free(p);}else{for(p=head,i=1;p->next&&inext);if(p->next){q=p->next;p->next=q->next;free(q);}}returnhead;}STUDENT*delect2(STUDENT*head,intnum){STUDENT*q,*p;for(p=head;p&&p->num!=num;q=p,p=p-
9、>next);if(p==NULL)printf("未找到");else{if(p==head)head=p->next;elseq->next=p->next;free(p);}returnhead;}STUDENT*search(STUDENT*head,intn){STUDENT*p;for(p=head;p;p=p->next)if(p->num==n)break;returnp;}STUDENT*destroy(STUDENT*head){STUDENT*p,*q;for(p=head;p;p=q
10、){q=p->next;free(p);}returnNULL;}voidprint(STUDENT*head){STUDENT*p;for(p=head;p;p=p->next)printf("%dt%f",p->num,p->score);}inttj1(STUDENT*head){STUDENT*p;inti;for(i=0,p=head;p;p=p->next,i++);