C语言链表输入输出

来源:百度知道 编辑:UC知道 时间:2024/05/25 03:14:41
我用tc运行,输入一组数据直接跳出,用Vs c++,输入0,0跳出~~~~~都没有输出键入的内容,这是为什么?编写的print函数有错吗?请高手赐教!
代码如下:
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)

struct student
{long num;
float score;
struct student *next;
};
int n;

struct student *creat(void)
{struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{ n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}

void print(struct student *head)
{ struct student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(he

struct student *creat(void)
{struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
//这里格式化输入二个数0,0
scanf("%ld,%f",&p1->num,&p1->score);
//head赋值为NULL
head=NULL;
//刚才输入的是0因此不满足条件一次循环都不走
while(p1->num!=0)
{ n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
//所以这时候head还是最初赋的值NULL自然打不出信息
return(head);
}

程序没错,跳出是因为你输入0,0