求救!数据结构!大一初学者(自学的)一段很简单的代码不知哪里有错?求学长和高人指点。

谜一样的Coder 2012-10-06 10:49:47
我是一个大一的学生刚开始自学数据结构,下面是上机实践遇到的一点小问题。
下面一段代码。只是实现简单的链表输入输出咋会出问题呢?好像是scanf()有问题但又不知哪儿有错?望各位指点一下!
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10
#define NULL 0
typedef struct student
{
int num;
int score;
struct student *next;
}student;

student *creat_list(student *L)
{
int i;
student *p,*r;
L=(student * )malloc(sizeof(student));
L->next=NULL;
r=L;
for(i=0;i<MAXSIZE;i++)
{
p=(student *)malloc(sizeof(student));
scanf("%d,%d",&p->num,&p->score);
p->next=NULL;
r->next=p;
r=p;
}
return L;
}
void print_list(student *L)
{
student *s;
s=L->next;
while(s->next!=NULL)
{
printf("%ld,%d",s->num,s->score);
s++;
}
}
void main()
{
student *L,*q;
L=NULL;
printf("输入相关数据:\n");
q=creat_list(L);
printf("相关数据为:\n");
print_list(q);
}
...全文
306 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
谜一样的Coder 2012-10-08
  • 打赏
  • 举报
回复
还是不行啊!崩溃了。新手太难了。
happytengfei 2012-10-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
用VC++6.0编的 0 error(s); 0 warning(s):就是执行不了,连输入都没输就直接跳出了。我个人估计可能scanf语句有问题;
[/Quote]

好吧,成功被你误导,scanf应该没错,我觉得最大的问题是print_list里的“s++”,程序非崩溃不可。
happytengfei 2012-10-07
  • 打赏
  • 举报
回复
scanf("%d,%d",&p->num,&p->score);查查书,确定& 与 ->的优先级大小。
q503959 2012-10-07
  • 打赏
  • 举报
回复
现在都在弄php..好久没碰过c了....重温一下感觉...顺便帮顶
谜一样的Coder 2012-10-07
  • 打赏
  • 举报
回复
用VC++6.0编的 0 error(s); 0 warning(s):就是执行不了,连输入都没输就直接跳出了。我个人估计可能scanf语句有问题;
happytengfei 2012-10-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

将“s++;”改成了“s=s->next;”了可还是不能执行!程序崩溃了,我也快要崩溃了
[/Quote]

不会吧,我这边运行的很好啊。
huangxy10 2012-10-07
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 2
#define NULL 0
typedef struct node
{
int num;
int score;
struct node *next;
}student;

student *creat_list( student *L)
{
int i;
student *p,*r;
L=(student * )malloc(sizeof(student));
L->next=NULL;
r=L;
for(i=0;i<MAXSIZE;i++)
{
p=(student *)malloc(sizeof(student));
scanf("%d,%d",&p->num,&p->score);
p->next=NULL;
r->next=p;
r=p;
}
return L;
}
void print_list(student *L)
{
student *s;
s=L->next;
while(s!= NULL) //自身不为空时,打印出来
{
printf("%d,%d\n",s->num,s->score);
//s++;
s = s->next;
}
}
void main()
{
student *L,*q;
L=NULL;
printf("输入相关数据:\n");
q=creat_list(L);
printf("相关数据为:\n");
print_list(q);
}
谜一样的Coder 2012-10-07
  • 打赏
  • 举报
回复
将“s++;”改成了“s=s->next;”了可还是不能执行!程序崩溃了,我也快要崩溃了
  • 打赏
  • 举报
回复
话说错误信息贴下吧
DeDeWo 2012-10-06
  • 打赏
  • 举报
回复
scanf("%d,%d",&p->num,&p->score);
这个这个,你输入时是不是没有输入 逗号了

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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