建链表一个奇怪的现象 急!
#include<stdio.h>
#include<malloc.h>
struct student
{
long num ;
float score;
struct student * next;
};
struct student *creat(void)
{
struct student *head ;
struct student *p1,*p2;
int n=0;
p1=(struct student *)malloc(sizeof(struct student));
scanf("%ld,%f",&p1->num,&p1->score);
p1->next=NULL;
p2=p1;
head=p1;
while(p1->num!=0)
{
n=n+1;
printf("%d\n",n);
p1=(struct student * )malloc(sizeof(struct student));
scanf("%ld,%f",&p1->num,&p1->score);
p1->next=NULL;
p2->next=p1;
p2=p1;
}
p2->next=NULL;
return(head);
}
void main()
{
creat();
}
问题:执行一次scanf("%ld,%f",&p1->num,&p1->score);但执行了2次printf("%d\n",n);也就是说当你输入1 23
会打印 1 2(应该只打印1啊)
thanks!