读书人

!数据结构!大一菜鸟(自学的)一段很

发布时间: 2012-10-19 16:53:37 作者: rapoo

求救!数据结构!大一初学者(自学的)一段很简单的代码不知哪里有错?求学长和高人指点。
我是一个大一的学生刚开始自学数据结构,下面是上机实践遇到的一点小问题。
下面一段代码。只是实现简单的链表输入输出咋会出问题呢?好像是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);
}

[解决办法]
scanf("%d,%d",&p->num,&p->score);
这个这个,你输入时是不是没有输入 逗号了
[解决办法]

探讨
用VC++6.0编的 0 error(s); 0 warning(s):就是执行不了,连输入都没输就直接跳出了。我个人估计可能scanf语句有问题;

[解决办法]
C/C++ code
#include<stdio.h>#include<stdlib.h>#define MAXSIZE 2#define NULL 0typedef 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);} 

读书人网 >软件架构设计

热点推荐