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

逻辑链表有关问题

2012-08-03 
逻辑链表问题#include stdio.h#include stdlib.h#include string.hstruct student{char name[30]in

逻辑链表问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student
{
 char name[30];
 int num;
 float score;
};

struct temp
{
 struct student*save;
 struct temp*next;
};

void create(struct student *head,struct temp *first);
void display (struct temp*first);

int main()
{
  struct student *head;
  struct temp *first;
  head=malloc(sizeof(struct student));
  first=malloc(sizeof(struct temp));
  create(head,first);
  display(first);
  system("PAUSE");
  return 0;
}

void create(struct student *head,struct temp *first)
{
 struct student *p,*q;
 struct temp *i,*j;
 int num;
 char name[30];
 float score;
 p=head;
 i=first;
 i->save=p;
 i->next=NULL;
  while(1)
  {
  printf("请输入学号:");
  scanf("%d",&num);
  if(num==0)
  {
  i->next=NULL;
  break;
  }
  else
  {
  printf("请输入姓名:");
  scanf("%s",name);
  printf("请输入分数:");
  scanf("%f",&score);
  p->num=num;
  p->score=score;
  strcpy(p->name,name);
  q=malloc(sizeof(struct student));
  j=malloc(sizeof(struct temp));
  i->next=j;
  i=j;
  p=q;
  }
  }
 return;
}

void display (struct temp*first)
{
 struct temp *i;
 i=first;
 while(i->next!=NULL)
 {
  printf("学号是:%d\n姓名是:%s\n分数是:%f\n",i->save->num,i->save->name,i->save->score);
  i=i->next;
 }
}
这是我写的链表 显示的时候只能显示第一组数据这是为什么啊???

[解决办法]
while循环里边还少一个:i->save=p;

[解决办法]

探讨

while循环里边还少一个:i->save=p;

热点排行