当前位置: 代码迷 >> C语言 >> [求助]单链表的问题
  详细解决方案

[求助]单链表的问题

热度:135   发布时间:2007-02-04 22:07:50.0
[求助]单链表的问题

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

#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编辑过]

搜索更多相关的解决方案: 单链  

----------------解决方案--------------------------------------------------------
把全部的代码发上来吧
----------------解决方案--------------------------------------------------------
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编辑过]


----------------解决方案--------------------------------------------------------

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编辑过]


----------------解决方案--------------------------------------------------------
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;
}
谢谢楼上的 朋友

----------------解决方案--------------------------------------------------------
scanf("%ld,%f",&p1->num,&p1->score);
P1你都还没有定义.

----------------解决方案--------------------------------------------------------
以下是引用无语的我在2007-2-6 1:04:42的发言:
scanf("%ld,%f",&p1->num,&p1->score);
P1你都还没有定义.

那里没有定义?


----------------解决方案--------------------------------------------------------

定义了,,,但是没分配空间。
{
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-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);
}

哥,你能不能看清楚在说
----------------解决方案--------------------------------------------------------

我认为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编辑过]


----------------解决方案--------------------------------------------------------
  相关解决方案