| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 805 人关注过本帖
标题:[求助]单链表的问题
只看楼主 加入收藏
love_hcy
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-12-24
收藏
 问题点数:0 回复次数:10 
[求助]单链表的问题

-写了个单链表,编译时没问题。但是运行时有错误。。
-建立链表那段都通不过。高手帮帮忙。万分感谢^_^。

#include <stdio.h>
#include <stdlib.h>
struct student {
int num;
float score;
struct student * next;
};

int main()
{
struct student *head=NULL;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student*)malloc(sizeof(struct student));
scanf("%d,%f",&p1->num,&p1->score);
while(p1->num)
{
n++;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
scanf("%d,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return 0;
}

[此贴子已经被作者于2007-2-4 22:22:12编辑过]

搜索更多相关主题的帖子: 单链 
2007-02-04 22:07
mp3aaa
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:2013
专家分:8
注 册:2006-2-15
收藏
得分:0 
把全部的代码发上来吧

羊肉串 葡萄干 哈密瓜!!
2007-02-04 22:11
mp3aaa
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:2013
专家分:8
注 册:2006-2-15
收藏
得分:0 
int main()
{
struct student *p1,*p2,*head;
head=(struct student*)malloc(sizeof(struct student));
p2=head;
scanf("%ld,%f",&p1->num,&p1->score);
while(p1->num)
{
p1=(struct student *)malloc(sizeof(struct student));
p2->next=p1;
p2=p1;
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return 0;
}
你拿回去运行以下 看看对不?

[此贴子已经被作者于2007-2-6 14:51:45编辑过]


羊肉串 葡萄干 哈密瓜!!
2007-02-04 22:19
alading664
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-1-25
收藏
得分:0 

int main()
{
struct student *p1,*p2,*head;
head=(struct student*)malloc(sizeof(struct student));
p1=head;
scanf("%ld,%f",&p1->num,&p1->score);
while(p1->num)
{
p1=(struct student *)malloc(sizeof(struct student));
p2->next=p1;//p2 好像还没定义是指向哪里啊?
p2=p1;
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return 0;
}

==================================================
我也写个链表,因为是初学,所以程序有点粗糙,LZ不要见笑

#include <stdio.h>
#include <stdlib.h>
int flag=1;
typedef struct student {
int num;
float score;
struct student * next;
} p_student;
int main()
{p_student *s,*p,*h;
while(1)
{
s=(p_student*)malloc(sizeof(p_student));
scanf("%d%f",&s->num,&s->score);
if(s==NULL||s->num==0)
{exit (0);
}
if(flag)
{h=s;
p=s;
h->next=NULL;
flag=0;
}
p->next=s;
p=s;
p->next=NULL;
}

}

[此贴子已经被作者于2007-2-5 0:02:55编辑过]

2007-02-05 00:02
mp3aaa
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:2013
专家分:8
注 册:2006-2-15
收藏
得分:0 
int main()
{
struct student *p1,*p2,*head;
head=(struct student*)malloc(sizeof(struct student));
p2=head;
scanf("%ld,%f",&p1->num,&p1->score);
while(p1->num)
{
p1=(struct student *)malloc(sizeof(struct student));
p2->next=p1;
p2=p1;
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return 0;
}
谢谢楼上的 朋友

羊肉串 葡萄干 哈密瓜!!
2007-02-05 00:03
无语的我
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2006-4-25
收藏
得分:0 
scanf("%ld,%f",&p1->num,&p1->score);
P1你都还没有定义.

2007-02-06 01:04
mp3aaa
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:2013
专家分:8
注 册:2006-2-15
收藏
得分:0 
以下是引用无语的我在2007-2-6 1:04:42的发言:
scanf("%ld,%f",&p1->num,&p1->score);
P1你都还没有定义.

那里没有定义?


羊肉串 葡萄干 哈密瓜!!
2007-02-06 14:51
独孤风
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2006-11-7
收藏
得分:0 

定义了,,,但是没分配空间。
{
long number;

struct contacts * phead,*pnew,*ptail;
phead=pnew=ptail=NULL;
while(1)
{

printf("your number:\n");
scanf("%ld",&number);

if(number==0)
break;
pnew=(struct contacts *)malloc(sizeof(struct contacts ));
printf("your name:\n");
scanf("%s",pnew->name);
pnew->number=number;
//pnew->next=NULL;
//ptail=pnew;
if(phead==NULL)
phead=pnew;
else

ptail->next=pnew;
ptail=pnew;

ptail->next=NULL;
}
return (phead);
}


用心去感受程序的美妙,无忧无虑的探寻精彩.
2007-02-07 13:42
mp3aaa
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:2013
专家分:8
注 册:2006-2-15
收藏
得分:0 
以下是引用独孤风在2007-2-7 13:42:11的发言:

定义了,,,但是没分配空间。
{
long number;

struct contacts * phead,*pnew,*ptail;
phead=pnew=ptail=NULL;
while(1)
{

printf("your number:\n");
scanf("%ld",&number);

if(number==0)
break;
pnew=(struct contacts *)malloc(sizeof(struct contacts ));
printf("your name:\n");
scanf("%s",pnew->name);
pnew->number=number;
//pnew->next=NULL;
//ptail=pnew;
if(phead==NULL)
phead=pnew;
else

ptail->next=pnew;
ptail=pnew;

ptail->next=NULL;
}
return (phead);
}

哥,你能不能看清楚在说


羊肉串 葡萄干 哈密瓜!!
2007-02-07 14:13
mzjllh
Rank: 2
来 自:江苏扬州
等 级:论坛游民
帖 子:76
专家分:41
注 册:2007-2-8
收藏
得分:0 

我认为love_hcy的程序基本没错,我只是在其基础上加了一些显示的东西,我想他(她)的问题可能出现在scanf函数中%d和%f之间的“,”上,有了逗号输入
时就必须有逗号,否则就出错,所以小弟提倡去掉逗号。
以下是修改后的代码,TC2通过,仅对原程序就着色部分稍作了修改:
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>/*或者是 #include <malloc.h>*/
#include <conio.h>
struct student {
int num;
float score;
struct student * next;
};

int main()
{
int i=1;
struct student *head=NULL;
struct student *p1,*p2;
int n=0;
clrscr();
p1=p2=(struct student*)malloc(sizeof(struct student));
printf("Please input the num and score of the student...\n");
scanf("%d%f",&p1->num,&p1->score);
while(p1->num!=0)
{
printf("Next one...\n");
n++;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%d%f",&p1->num,&p1->score);
}
free(p1);
p2->next=NULL;
/*遍历并显示链表*/
while(i<=n)
{
if(i==1)
{
p1=head;
p2=p1;
}
else
p1=p1->next;
printf("Student %d:\tnum:%5d\t\tscore:%f\n",i,p1->num,p1->score);
i++;
}
/*遍历结束*/
printf("Press any key to quit...");
while(!kbhit());

return 0;
}
另外,在输入num项值时,如果数据大于或等于32768时出现溢出,可考虑使用字符数组或字符指针以进一步完善。

[此贴子已经被作者于2007-2-8 23:06:56编辑过]

2007-02-08 22:56
快速回复:[求助]单链表的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015249 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved