例题:动态链表的建立与输出

 1 /*动态链表的建立与输出*/
 2 #include<stdio.h>
 3 #include<malloc.h>
 4 #define N sizeof(struct Student)
 5 struct Student
 6 {
 7     int num;
 8     float score;
 9     struct Student *next;
10 };
11 void print(struct Student *head);
12 struct Student *creat(void);
13 int main(void)
14 {
15     struct Student *pt;
16     pt=creat();
17     print(pt);
18     return 0;
19 }
20 struct Student *creat(void)
21 {
22     struct Student *p1,*p2,*head;
23     int n=0;
24     p1=p2=(struct Student *)malloc(N);
25     head=NULL;
26     printf("Please input the NUM and score,input '0,0' end:\n");
27     scanf("%d,%f",&p1->num,&p1->score);
28     while (p1->num!=0)
29     {
30         n=n+1;
31         if(n==1) head=p1; else p2->next=p1;
32         p2=p1;
33         p1=(struct Student *)malloc(N);
34         scanf("%d,%f",&p1->num,&p1->score);
35     }
36     p2=NULL;
37     return (head);
38 }
39 void print(struct Student *head)
40 {
41     struct Student *p;
42     p=head;
43     if(head!=NULL)
44     {
45         do 
46         {
47             printf("NUM:%2d\t Score:%5.2f\t\n",p->num,p->score);
48             p=p->next;
49         }while (p!=NULL);
50     }
51 }

 

posted @ 2013-11-22 00:16  ASMLearner  阅读(461)  评论(0)    收藏  举报