C语言链表的输入输出

#include<stdio.h>#include<malloc.h>#defineNULL0#defineLENsizeof(structstudent)structs... # include <stdio.h>
# include <malloc.h>
# define NULL 0
# define LEN sizeof(struct student)
struct student
{int num;
int score;
struct student *next;
};
int n;
struct student *creat(void)
{struct student *head;
struct student *p1,*p2;
n=0;
scanf("%d,%d",&p1->num,&p1->score);
p1=p2=(struct student*)malloc(LEN);
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,%d",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void print (struct student *head)
{ struct student *p;
p=head;
if (head!=NULL)
do
{printf ("%d %d\n",p->num,p->score);
p=p->next;
}while (p!=NULL);
}
void main ()
{struct student *creat();
void print ();}

程序编写如上。。。测试没有错,但是运行的时候连输入都无法执行,不知道是怎么回事。。。。求救啊啊啊~~~~
先多谢大家了,要是有好的答案,我一定会加分的~~~
展开
 我来答
百度网友64de9a1
2012-09-04
知道答主
回答量:9
采纳率:0%
帮助的人:6.9万
展开全部
# include <stdio.h>
# include <malloc.h>
# define NULL 0
# define LEN sizeof(struct student)
struct student
{int num;
int 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,%d",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if (n==1)head=p1;
else {
p1=(struct student *)malloc(LEN);
p1->next=NULL;
p2->next=p1;
p2=p1;
scanf("%d,%d",&p1->num,&p1->score);
}
}
return(head);
}
void print (struct student *head)
{ struct student *p;
p=head;
do
{printf ("%d %d\n",p->num,p->score);
p=p->next;
}while (p!=NULL);
}
void main ()
{
print (creat());
}

代码修改好,主要注意在你没malloc空间就给结构体传值了,所以会出错
scanf("%d,%d",&p1->num,&p1->score);
394733890
2012-09-04 · 超过11用户采纳过TA的回答
知道答主
回答量:33
采纳率:0%
帮助的人:28.1万
展开全部
主函数改为
void main ()
{
struct student *head=creat();
print (head);
}

scanf("%d,%d",&p1->num,&p1->score);
p1=p2=(struct student*)malloc(LEN);
两行对调
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
xoalyg
2012-09-13 · TA获得超过4178个赞
知道大有可为答主
回答量:2356
采纳率:100%
帮助的人:2384万
展开全部
/*
num(0 to quit) = 50286
score = 98
num(0 to quit) = 33654
score = 87
num(0 to quit) = 78954
score = 69
num(0 to quit) = 33752
score = 86
num(0 to quit) = 43256
score = 90
num(0 to quit) = 0
50286 98
33654 87
78954 69
33752 86
43256 90
Press any key to continue
*/
#include <stdio.h>#include <malloc.h>
#define LEN sizeof(struct student)
struct student {
int num;
int score;
struct student *next;
};

struct student *creat(void) {
int n = 0,num;
struct student *head,*p;
head = p = (struct student*)malloc(LEN);
while(1) {
printf("num(0 to quit) = ");
scanf("%d",&num);
if(num == 0) break;
p->next = (struct student *)malloc(LEN);
p->next->num = num;
printf("score = ");
scanf("%d",&p->next->score);
p = p->next;
}
p->next = NULL;
return(head);
}

void print(struct student *head) {
struct student *p;
for(p = head->next; p; p = p->next) {
printf("%d %d\n",p->num,p->score);
}
}

void main () {
struct student *head = creat();
print(head);}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式