请高手帮忙修改下面的C语言链表程序

来源:百度知道 编辑:UC知道 时间:2024/06/01 13:55:29
下面是一个C语言写的链表程序,在运行时在删除链表数据时成了死循环,请高手帮我看下错误在那。谢谢!!!!!!1
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct number)

struct number
{ char num;
struct number * next; };

int n;
struct number * creat(void)
{ struct number * head;
struct number * p1,* p2;
n=0;
p1=p2=(struct number *)malloc(LEN);
scanf("%c",&p1->num);
head=NULL;
while(p1->num!='0')
{ n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct number *)malloc(LEN);
scanf("%c",&p1->num); }

p2->next=NULL;
return(head);
}

void print( struct number * head)
{ struct number * p;
printf("\n Now,these %d records are:\n",n);
p=head;
if(head!=NULL)
do{ printf("%4c",

struct number * del( struct number * head,char del_num)
{ struct number * p1,* p2;
if(head==NULL)
{ printf("\n The list null!\n"); goto end;}
p1=head;

while(del_num!=p1->num&&p1->next!=NULL)
{ p2=p1; p1=p2->next; }
if(del_num==p1->num)
{ if(p1==head)head=NULL; (这句改了)
else p2->next=p1->next;
printf("delete:%c \n",del_num);
n=n-1;
}
else printf("%c not been found!\n",del_num);
end:return(head);
}