|
我的代码如下,编译没问题,但运行时却出错。我怀疑是指针的错误,请高手指教。^_^
#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
void createlist(struct node *head)
{
int e;
struct node *p;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
printf("输入非零数据(遇零结束)\n");
scanf("%d",&e);
while(e!=0)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=e;
p->next=head->next;
head->next=p;
scanf("%d",&e);
}
}
void display(struct node *head)
{
while(head->next)
{
printf("%d\t ",head->next->data);
head=head->next;
}
printf("\n");
}
int main()
{
struct node head;
createlist(&head);
display(&head);
return 0;
} |
|