struct student *delete(struct student *head)
{
struct student *p1=head,*p2;
if(head==NULL)
{
printf("List is null!\n");
}
while((p1->score>=50)&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->score<50)
{
if(p1==head)
{
p2=p1->next;
free(head);
return p2;
}
else
{
p2->next=p1->next;
free (p1);
}
n=n-1;
}
else printf("No student's score under 50!\n");
return head;
}
需要删除五十分以下的节点,可是我的只能删除一个结点,无法删除多个,那个大牛能帮忙看看原因啊!!!
额,我知道啊,可是一直没循环成功···所以才来问的····
追答while((p1->score>=50)&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
你不觉得这个有点问题吗?
如果你第一个人的分数就不到50。。自然就不会进入循环。。更改成
while(p1->next!=NULL)
你的while循环实际上只是在一步一步的移指针。。把你删除节点的代码段,剪切进循环内部。
注意检查下判断条件。