C程序有点小问题,求助。。

来源:百度知道 编辑:UC知道 时间:2024/05/17 11:01:31
#include<stdio.h>
void main()
{typedef struct student
{int data; //data是学生分数
int num; //num是学号
struct student *p;
}studentList,*listNode;
listNode head,p1,p2;
p1=p2=head=(listNode)malloc(sizeof(struct student));
head->data=-1;
head->num=-1;
do
{
p1=(listNode)malloc(sizeof(studentList));
p2->p=p1;
printf("input fengshu and xuehao\t");
scanf("%d,%d\n",&p1->data,&p1->num);
p2=p1;
}while(p1->num);
if(!p1->num)
{p2->p=NULL;
free(p1);
}
p1=head->p;
while(p1)
{printf("student[%d]'s score is %d\n",p1->num,p1->data);
p1=p1->p;
}
free(head);
}
程序功能很简单,就是用链式存储方式,输入学分,学号,然后输出,并约定学号为0时输入完毕。程序没有语法,逻辑错误,可运行后出现了以下情况:
input fengshu and xuehao 60,1(回车)
70,2(回车) //此为问题1:input fengshu and xuehao没了?
input fengshu and x

改一下:
do
{
p1=(listNode)malloc(sizeof(studentList));
p2->p=p1;
printf("input fengshu and xuehao\t");
scanf("%d,%d",&p1->data,&p1->num);
p2=p1;
}while(p1->num);

怎么百度上的代码看起来都这么费劲呢``