读书人

简单的动态链表异常

发布时间: 2012-06-02 14:16:14 作者: rapoo

简单的动态链表错误
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alloc.h>

struct student
{
int num;
char name[30];
struct student *next;
};

int main()
{
struct student *head;
head=(struct student*)malloc(sizeof(struct student));
head->next=NULL;
create(head);
display(head);
system("pause");
return 0;
}

void create(struct student*p)//创建函数
{
struct student*q;
int num=1;
char name[30];
q=p;
while(1)
{
printf("请输入学号:");
scanf("%d",&num);
if(num!=0)
{
printf("\n请输入姓名");
scanf("%s",name);
strcpy(p->name,name);
p->num=num;
q=(struct student*)malloc(sizeof(struct student));
p->next=q;
}
else
{
p->next=NULL;
break;
}
}
}

void display(struct student*p)//输出函数
{
while(p!=NULL)
{
printf("%d%s",p->num,p->name);
p=p->next;
}
}
编译通过,但是运行到输出这一步就出错,这是为什么呢???

[解决办法]

C/C++ code
main()中的create(head)也要改成create(&head); 

读书人网 >C语言

热点推荐