首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

简单的动态链表异常

2012-06-02 
简单的动态链表错误#include stdio.h#include stdlib.h#include string.h#include alloc.hstruct

简单的动态链表错误
#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); 

热点排行