关于链表的一点小先问题请教
#include<stdio.h>
#include<malloc.h>
struct list
{
int data;
struct list *next;
};
int main()
{
int value;
struct list *head;
head=(struct list *)malloc(sizeof (struct list));
struct list *p;
scanf("%d",&value);
head->data=value;
p=head->next;
while(scanf("%d",&value)&&value)
{
p->data=value; ///////////////这里有问题
p=p->next;
}
p->next=NULL; return0;
}
当运行到标记那里时就出现了问题。. 可是我怎么都想不通为什么会不能运行..如果有内存详解更好。还望各位指教
当
[解决办法]
#include <stdio.h >
#include <malloc.h >
struct list
{
int data;
struct list *next;
};
int main()
{
int value;
struct list *head;
head=(struct list *)malloc(sizeof (struct list));
struct list *p;
p=head;
scanf("%d",&value);
head->data=value;
while(scanf("%d",&value)&&value)
{
p->next=(struct list*)malloc(sizeof(struct list));//必须分配内存,而且p->next必须链接上下一级结点!
p=p->next;
p->data=value;
}
p->next=NULL;
for(p=head;p!=NULL;p=p->next)
printf("%d ",p->data);
printf("\n");
return 0;
}