资源描述:
《单链表的创建、插入、删除、打印、查找》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#includestructlinklist{intnum;structlinklist*next;};structlinklist*creatlist(structlinklist*head){structlinklist*p=NULL,*q=NULL;inti=1,n;printf("请输入链表的长度:");scanf("%d",&n);printf("请输入链表信息:",n);while(i<=n){p=(structlinklist*)malloc(sizeof(structlinklist));scan
2、f("%d",&p->num);p->next=NULL;if(i==1){head=p;q=p;}else{q->next=p;q=p;}i++;}returnhead;}voidlistprint(structlinklist*head){structlinklist*p;p=head;printf("链表信息:");while(p!=NULL){printf("%d",p->num);p=p->next;}printf("");}intlistsearch(structlinklist*head,intno){structlinklist*p=NULL;int
3、j=0;p=head;while(p!=NULL){j++;if(no==j){break;}p=p->next;}if(p!=NULL){return1;}else{return0;}}structlinklist*listinsert(structlinklist*head){structlinklist*p=NULL,*q=NULL,*p0=NULL;q=head;intj,i=1;p=(structlinklist*)malloc(sizeof(structlinklist));printf("请输入要插入的结点的信息和插入位置:");scanf("%d%d",
4、&p->num,&j);p->next=NULL;if(head==NULL){head=p;}else{while(q!=NULL){++i;if(j==i){p->next=q->next;q->next=p;break;}else{q=q->next;}}printf("插入成功!");}returnhead;}structlinklist*listdelete(structlinklist*head){structlinklist*p=NULL,*q=NULL;intnum;p=head;printf("请输入删除的结点:");scanf("%d",&num
5、);if(listsearch(head,num)==1){while(p->num!=num){q=p;p=p->next;}if(p==head){head=p->next;}else{q->next=p->next;}printf("删除成功!",num);}else{printf("%d不存在!",num);}listprint(head);returnhead;}intmain(){structlinklist*head;intselect;intx,num;while(1){printf("********************************
6、*");printf("*1.创建2.插入3.删除*");printf("*4.打印5.查询0.退出*");printf("*********************************");printf("请选择功能:");scanf("%d",&select);if(select==1){head=creatlist(head);}if(select==2){head=listinsert(head);}if(select==3){head=listdelete(head);}if(select==4){listprint(head);}if(s
7、elect==5){printf("请输入要寻找的结点的位置:");scanf("%d",&num);x=listsearch(head,num);}if(select==0){break;}}}