链表问题,帮忙看下!谢谢
程序目的是当输入一个age,当与链表中结点的年龋相同是,将这个结点删点,但是我这个程序如果链表中有2个相同年龋的结点的话,只有删掉一个,不知道哪出问题了!!帮忙看下啊谢谢了!#define NULL 0
#define LEN sizeof(struct student)
struct student
{char num[6];
char name[10];
char sex[2];
int age;
struct student *next;
}stu[10];
main()
{int i,flag=1,length,age1;
struct student *p,*head,*p1;
while(flag==1)
{printf("Input length of list(<10):");
scanf("%d",&length);
if(length<10)
flag=0;
}
p1=(struct student *) malloc(LEN);
head=p1;
for(i=0;i<length;i++)
{p=p1;
printf("NO.:");
scanf("%s",p->num);
printf("name:");
scanf("%s",p->name);
printf("sex:");
scanf("%s",p->sex);
printf("age:");
scanf("%d",&p->age);
p1=(struct student *) malloc(LEN);
p->next=p1;
}
p->next=NULL;
p=p1=head;
printf("Input age:");
scanf("%d",&age1);
while(p!=NULL)
{if(age1==p->age)
{if(p==head)
head=p->next;
else if(p->next==NULL)
p1->next=NULL; 这里出问题了但不知道出在哪??
else p1->next=p->next;
}
p1=p;
p=p->next;
}
printf("NO. name sex age\n");
p=head;
while(p!=NULL)
{printf("%-3s%8s%6s%6d\n",p->num,p->name,p->sex,p->age);
p=p->next;
}
}