读书人

看不出链表疏失的地方但是只能输入一

发布时间: 2012-10-05 15:34:34 作者: rapoo

看不出链表出错的地方,但是只能输入一个节点的数据,而且会停止工作,初级求教。
#include<stdio.h>
#include<malloc.h>
#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("%d%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;

p1=(struct student*)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
p2=p1;
}
p2->next=NULL;
return (head);
};

void print(struct student *head)
{
struct student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%d %f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
};

void main()
{
struct student *head;
head=creat();
print(head);
}

[解决办法]
代码逻辑错误外加scanf有的没有逗号
head=p1=(struct student *)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
//head=NULL;
while(p1->num!=0)
{
n=n+1;

p2=(struct student*)malloc(LEN);
scanf("%d,%f",&p2->num,&p2->score);
p1->next = p2;
p1=p2;
}
p2->next=NULL;
return (head);
};

[解决办法]
1,2请不要误人子弟好吗?多加,和不加有什么区别,应该是逻辑错误的问题,代码没注释,又没有对代码的解释说实话没人会来看的。
[解决办法]

探讨
1,2请不要误人子弟好吗?多加,和不加有什么区别,应该是逻辑错误的问题,代码没注释,又没有对代码的解释说实话没人会来看的。

读书人网 >C语言

热点推荐