用c创建链表时出错了:(

asvaboy1980 2002-10-16 11:01:43
程序源代码是:
#include"stdio.h"
#include<alloc.h>
typedef struct node{
int data;
struct node *next;
} Listnode;
typedef Listnode *Linklist;
Linklist creatlist(head)
Linklist head;
{ int a;
Listnode *s;
head=NULL;
printf("Input s->data:\n");
while(1)
{ scanf("%d",&a);
if(a==0) break;
s=(Listnode*)malloc(sizeof(Listnode));
s->data=a;
s->next=head;
head=s;
}
return head;
}
main()
{ Linklist p;
Linklist creatlist(p);
while(p!=NULL)
{ prinf("%d",p->data);
p=p->next;
}
}

在Tc下编译时,说有一个错误在26行,求救:(
...全文
67 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hello_wyq 2002-10-17
  • 打赏
  • 举报
回复
linklist是指针,你又传给它的是值,而不是地址,做了半天p值还是没有改变啊。因此要么船引用,要么传指针的地址.

real_chang 2002-10-16
  • 打赏
  • 举报
回复
你既然已经定义了
Linklist p = NULL;
那么到
Linklist creatlist(p);
时调用Linklist creatlist(head)
Linklist head;
因为名称相同,即P被定义了两次,当然会出错
zxm954712 2002-10-16
  • 打赏
  • 举报
回复
通上
zjw2723114 2002-10-16
  • 打赏
  • 举报
回复
当然错啦
应该为creatlist(p);
去掉Linklist
双杯献酒 2002-10-16
  • 打赏
  • 举报
回复
加上
#include <malloc.h>
看看
blue_coco 2002-10-16
  • 打赏
  • 举报
回复
每次将新加节点放在最前面。

Linklist creatlist(head)
Linklist head;
{
int a;
Listnode *s;

printf("Input s->data:\n");
while(1)
{
scanf("%d",&a);
if(a==0) break;
s=(Listnode*)malloc(sizeof(Listnode));
if (NULL == s) break;
s->data = a;
if (NULL == head)
{
s->next = NULL;
}
else
{
s->next = head;
}
head = s

}
return head;
}

main()
{
Linklist p = NULL;

Linklist creatlist(p);
while(p!=NULL)
{ prinf("%d",p->data);
p=p->next;
}
}
Anderes 2002-10-16
  • 打赏
  • 举报
回复
你的头文件都是错误的!!!!
zhi_chong 2002-10-16
  • 打赏
  • 举报
回复
在main()中是调用creatlist(p)而不是再次定义函数。
因为你已经定义过了

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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