在void UMS_444()中,while的循环条件是p->next!=NULL。p 指向倒数第二个节点,而 p->next 是最后一个节点,可以正常进入while。之后p指向最后一个结点,最后一个结点next为NULL,不符合while的条件,按理说不会进入循环,最后一个结点的数据也无法输出。但在我的测试中,最后一个数据仍然能正常输出,求高人指点
node1* user_link()
{
node1 *head=(node1 *)malloc(sizeof(node1));
node1 *last;
head->next=NULL;
last=head;
FILE *fp;
fp=fopen("user.txt","r");
while(!feof(fp))
{
node1 *s=(node1 *)malloc(sizeof(node1));
fscanf(fp,"%d %s %d",&s->data.num,s->data.mima,&s->data.leixing);
last->next=s;
s->next=NULL;
last=s;
}
fclose(fp);
return head;
}
void UMS_444()
{
node1 *head=user_link();
node1 *p=head->next;
while(p->next!=NULL)
{
printf("%d ",p->data.num);
printf("%s ",p->data.mima);
printf("%d ",p->data.leixing);
printf("\n");
p=p->next;
}
}