看不出链表出错的地方,但是只能输入一个节点的数据,而且会停止工作,初级求教。

小小白杨123 2012-09-19 03:59:38
#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);
}
...全文
127 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tcm86079572 2012-09-20
  • 打赏
  • 举报
回复
1,2请不要误人子弟好吗?多加,和不加有什么区别,应该是逻辑错误的问题,代码没注释,又没有对代码的解释说实话没人会来看的。
小小白杨123 2012-09-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
代码逻辑错误外加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",……
[/Quote]

改了之后还是不行啊
小小白杨123 2012-09-20
  • 打赏
  • 举报
回复
谢谢大家的建议,我已经解决问题了。
Dany 2012-09-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
1,2请不要误人子弟好吗?多加,和不加有什么区别,应该是逻辑错误的问题,代码没注释,又没有对代码的解释说实话没人会来看的。
[/Quote]
连scanf函数都搞不清楚还在这里现
scanf("%d,%d,%d",&a,&b,&c);中%d,%d,%d之间有逗号,在输入数据时也要加逗号,如果去掉逗号,输入时就不用逗号,而用空格,tab键或回车键将各个数据隔开。
另一个更重要的好处是
方便代码阅读。
Dany 2012-09-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
引用 1 楼 的回复:
代码逻辑错误外加scanf有的没有逗号
head=p1=(struct student *)malloc(LEN);
scanf("%d,%f",&amp;p1->num,&amp;p1->score);
//head=NULL;
while(p1->num!=0)
{
n=n+1;

p2=(struct student*)malloc(……
[/Quote]
不明白你这里的scanf函数到底是想做什么。本人提供的代码经验证是绝对可以的。
Dany 2012-09-19
  • 打赏
  • 举报
回复
代码逻辑错误外加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);
};

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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