建链表一个奇怪的现象

shendu21 2010-11-28 09:58:00
#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();
}
...全文
113 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shendu21 2010-11-28
  • 打赏
  • 举报
回复
就是说每执行次printf("%d\n",n);也执行次scanf("%ld,%f",&p1->num,&p1->score);
但执行结果是执行一次scanf("%ld,%f",&p1->num,&p1->score);但执行了2次printf("%d\n",n);
Dstnoe 2010-11-28
  • 打赏
  • 举报
回复
楼主能不能说清楚一点,怎么没下文了呢?、
shendu21 2010-11-28
  • 打赏
  • 举报
回复
问题:没两次while只执行一次scanf("%ld,%f",&p1->num,&p1->score);也就是说当你输入1 23
会打印 1 2(应该只打印1啊)
thanks!

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧