在线请求代码分析
arkor 2010-12-27 03:34:06 代码中的错误分析
正确的代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct list
{
int data;
struct list *next;
};
typedef struct list *link;
typedef struct list node;
link creat(int n)
{
int i,x;
link head,p,s;
head=(node*)malloc(sizeof(node));
if(head==NULL)
{
printf("空子集");
exit(0);
}
p=head;
for(i=0;i<n;i++)
{
s=(node*)malloc(sizeof(node));
printf("请输入一个值:");
scanf("%d",&x);
s->data=x;
p->next=s;
p=s;
}
s->next=NULL;
return head;
}
void output(link head)
{
link pt;
pt=head->next;
while(pt)
{
printf("%5d",pt->data);
pt=pt->next;
}
}
int main(void) {
link head;
head=creat(4);
output(head);
}
当我在output()函数里加入一条语句:head=(node*)malloc(sizeof(node))。
它就编译不出来,请问这是什么原因啊,能否解释一下,谢谢。