欢迎来到天天文库
浏览记录
ID:58460979
大小:16.50 KB
页数:2页
时间:2020-09-03
《建立和删除动态链表.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#include#defineLENsizeof(structstudent)structstudent*creat();structstudent*del(structstudent*head,intnum);voidprint(structstudent*head);###########################################structstudent{intnum;floatscore;
2、structstudent*next;};intn;###########################################voidmain(){structstudent*stu,*p;intn;stu=creat();p=stu;print(p);printf("pleaseenterthenumtodelete:");scanf("%d",&n);print(del(p,n));}###########################################structstu
3、dent*creat(){structstudent*head;structstudent*p1,*p2;p1=p2=(structstudent*)malloc(LEN);printf("pleaseenterthenum:");scanf("%d",&p1->num);printf("pleaseenterthescore:");scanf("%f",&p1->score);head=NULL;n=0;while(p1->num){n++;if(1==n){head=p1;}else{p2->next=
4、p1;}p2=p1;p1=(structstudent*)malloc(LEN);printf("pleaseenterthenum:");scanf("%d",&p1->num);printf("pleaseenterthescore:");scanf("%f",&p1->score);}p2->next=NULL;returnhead;}###########################################voidprint(structstudent*head){structstu
5、dent*p;printf("thereare%dreconds!",n);if(head){p=head;do{printf("学号%d的成绩为:%f",p->num,p->score);p=p->next;}while(p);}}structstudent*del(structstudent*head,intnum){structstudent*p1,*p2;if(NULL==head){printf("thislistisnull");gotoEND;}p1=head;while(
6、p1->num!=num&&p1->next!=NULL){p2=p1;p1=p1->next;}if(num==p1->num){if(p1==head){head=p1->next;}else{p2->next=p1->next;}printf("deleteNo.%dsucceed!",num);n=n-1;}else{printf("%dnotbeenfound!",num);}END:returnhead;}
此文档下载收益归作者所有