结构体与链表

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<malloc.h>
 5 #define N 3
 6 #define LEN sizeof(struct grade)
 7 struct grade
 8 {
 9     char no[7];
10     int score;
11     struct grade *next;
12 };
13 struct grade *create(void)
14 {
15 
16     struct grade  *head=NULL,*new,*tail;
17     int i ;
18     for( i=1;i<=N;i++)
19     {
20 
21         new=(struct grade*)malloc(LEN);
22         printf("please int no\n");
23         scanf("%s",new->no);
24         if(strcmp(new->no,"000000")==0)
25         {
26             free(new);
27             break;
28         }
29         printf("please int score\n");
30         scanf("%d",&new->score);
31         new->next=NULL;
32         if(i==1)  head=new;
33         else    tail->next=new;
34         tail=new;
35     }
36     return head;
37 
38 }
39 main()
40 {
41 
42     struct grade *p;
43     int i;
44     p=create();
45     if(p!=NULL)
46 
47 
48        for(i=1;i<=N;i++)
49     {
50         printf("%s:%d\n",p->no,p->score);
51         p=p->next;
52     }
53   free(p);
54 }

 

posted @ 2017-01-05 15:37  legenda  阅读(347)  评论(0编辑  收藏  举报