例题:动态建立链表

 1 #include<stdio.h>
 2 #define LEN sizeof(struct Student)
 3 struct Student
 4 {
 5     int num;
 6     float score;
 7     struct Student *next;
 8 };
 9 int n=0;
10 struct Student *creat()
11 {
12     struct Student *p1,*p2,*head;
13     p1=p2=(struct Student *)malloc(LEN);
14     head=NULL;
15     printf("请输入学生的学号和成绩,输入0,0结束\n");
16     scanf("%d%f",&p1->num,&p1->score);
17     while(p1->score!=0)
18     {
19         n=n+1;
20         if(n==1) head=p1; else p2->next=p1;
21         p2=p1;
22         p1=(struct Student *)malloc(LEN);
23         scanf("%d%f",&p1->num,&p1->score);
24     }
25     p2->next=NULL;
26     return (head);
27 }
28 int main(void)
29 {
30     struct Student *pt;
31     pt=creat();
32     do
33     {
34         printf("学号:%5.2d\t 成绩:%5.2f\t\n",pt->num,pt->score);
35         pt=pt->next;
36     }while(pt!=NULL);
37     return 0;
38 }

 

posted @ 2013-11-21 23:36  ASMLearner  阅读(282)  评论(0)    收藏  举报