现在的位置: 首页 > 综合 > 正文

链表的建立

2014年01月09日 ⁄ 综合 ⁄ 共 591字 ⁄ 字号 评论关闭

#include<stdio.h>
#include<malloc.h>

struct student
{
 int num;
 float score;
 struct student *next;
};
#define len sizeof(struct student)
int main()
{
 int n=0;
 struct student *head,*p1,*p2;
 printf("input num score :/n");
 p1=(struct student*) malloc(len);
 scanf("%d %f",&p1->num,&p1->score);
 
 while(p1->num!=0)
 {
  n++;
  if(n==1)
  {
   head=p1;
   p2=p1;
  }
  else
  {
   p2->next=p1;
   p2=p1;
  
  }
 printf("input num score :/n");
 p1=(struct student*) malloc(len);
 scanf("%d %f",&p1->num,&p1->score);

 }
 p2->next=NULL;
 printf("the link is :/n");
 while(head!=NULL)
 {
 
  printf("%d %f/n",head->num,head->score);
  head=head->next;
 }
return 0;
}

抱歉!评论已关闭.