欢迎来到天天文库
浏览记录
ID:52783438
大小:14.50 KB
页数:3页
时间:2020-03-30
《C语言链表的构建、输出、遍历、删除、插入.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#include#defineLENsizeof(structStudent)structStudent{intnum;structStudent*next;};structStudent*creat(){structStudent*p1,*p2,*head;head=NULL;p1=p2=malloc(LEN);scanf("%d",&p1->num);if(p1->num!=0){head=p1;do{p2=p1;p1=malloc(LEN);p2
2、->next=p1;scanf("%d",&p1->num);}while(p1->num!=0);p2->next=NULL;}returnhead;}voidprint(structStudent*head){structStudent*p=head;if(head==NULL)printf("LISTNULL!");elsewhile(p!=NULL){printf("%dt",p->num);p=p->next;}}structStudent*del(structStudent*head,int
3、num){structStudent*p1,*p2;if(head==NULL)printf("LISTNULL!");else{p1=head;while(p1!=NULL&&p1->num!=num)//不可颠倒{p2=p1;p1=p1->next;}if(p1==NULL)printf("Itdoesn'texist!");//不能先判断是否找到该值else{if(p1==head)head=p1->next;elsep2->next=p1->next;free(p1);}}returnhe
4、ad;}structStudent*insert(structStudent*head,intnum){structStudent*p1=head,*p2,*p;while(p1!=NULL&&p1->numnext;}if(p1==head){head=malloc(LEN);head->num=num;head->next=p1;}else{p2->next=p=malloc(LEN);p->num=num;p->next=p1;}returnhead
5、;}voidmain(){structStudent*head;intdelnum,insertnum;printf("Pleaseinputallstudents'numberfromsmalltobig(endwith0):");head=creat();print(head);printf("");printf("Thenumberyouwanttodelete:");scanf("%d",&delnum);head=del(head,delnum);printf("Thenumberyouwa
6、nttoinsert:");scanf("%d",&insertnum);head=insert(head,insertnum);printf("Pleasechecktheresult:");print(head);printf("");}
此文档下载收益归作者所有