这个是c语言里构建链表的程序,为什么不能执行啊

[复制链接]
查看11 | 回复2 | 2011-4-22 06:03:12 | 显示全部楼层 |阅读模式
#includestdio.h
#includestring.h
#includemalloc.h
typedef struct simple
{
struct
{
int coef;
int expn;
}simple2 ;
struct simple * next;
}*linklist;
linklist creat()
{
linklist p,q,head;
int n=0;
p=NULL;
q=(linklist)malloc(10*sizeof(int));
head=q;
printf(\"please input the coef\");
scanf(\"%d\",simple2.coef);
printf(\"please input the expn\");
scanf(\"%d\",simple2.expn);
while((p-simple2.coef!=123456)simple2.expn!=123456))
{

p=q;

q=(linklist)malloc(10*sizeof(int));

p-next=q;

printf(\"please input the ceof\");

scanf(\"%d\",simple2.coef);

printf(\"please input the expn\");

scanf(\"%d\",simple2.expn);
}
return head;
}
void main()
{
creat();
}这是代码,
回复

使用道具 举报

千问 | 2011-4-22 06:03:12 | 显示全部楼层
<pre id=\"best-answer-content\" class=\"reply-text mb10\">你的while语句中的p没有指向,所以会出现内存错误

















<h4 class=\"ask\">追问





<pre class=\"replyask-text\" id=\"content-5090881\">p已经指向了啊,用 p=(linklist)malloc(50);这个语句指向了啊?
回复

使用道具 举报

千问 | 2011-4-22 06:03:12 | 显示全部楼层
<pre class=\"replyask-text\" id=\"content-5102616\">你这段代码好像没写哦?你把前面的q=(linklist)malloc(10*sizeof(int));改成p=q=(linklist)malloc(10*sizeof(int));就对了。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行