实验11-2-2 学生成绩链表处理

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 struct stud_node {
 5     int    num;
 6     char   name[20];
 7     int    score;
 8     struct stud_node* next;
 9 };
10 
11 struct stud_node* createlist();
12 struct stud_node* deletelist(struct stud_node* head, int min_score);
13 
14 int main()
15 {
16     int min_score;
17     struct stud_node* p, * head = NULL;
18 
19     head = createlist();
20     scanf_s("%d", &min_score);
21     head = deletelist(head, min_score);
22     for (p = head; p != NULL; p = p->next)
23         printf("%d %s %d\n", p->num, p->name, p->score);
24 
25     return 0;
26 }
27 
28 /* 你的代码将被嵌在这里 */
29 struct stud_node* createlist()
30 {
31     struct stud_node* head, *tail, *p;
32     int num;
33 
34     head = tail = NULL;
35     scanf_s("%d", &num);
36     while (num != 0)
37     {
38         p = (struct stud_node*)malloc(sizeof(struct stud_node));
39         p->num = num;
40         scanf_s(" %s %d", p->name,20, &(p->score));
41         p->next = NULL;
42 
43         if (head == NULL)
44         {
45             head = p;
46             tail = p;
47         }
48         else
49         {
50             tail->next = p;
51             tail = p;
52         }
53         
54         scanf_s("%d", &num);
55     }
56 
57     return head;
58 }
59 struct stud_node* deletelist(struct stud_node* head, int min_score)
60 {
61     struct stud_node* p, * q;
62 
63     while ((head != NULL) && (head->score < min_score))
64     {
65         p = head;
66         head = head->next;
67         free(p);
68     }
69 
70     if (head != NULL)
71     {
72         q = head;
73         p = head->next;
74 
75         while (p != NULL)
76         {
77             if (p->score < min_score)
78             {
79                 q->next = p->next;
80                 free(p);
81                 p = q->next;
82             }
83             else
84             {
85                 q = p;
86                 p = p->next;
87             }
88         }
89     }
90 
91     return head;
92 }

 

posted @ 2020-07-18 16:28  jason2018  阅读(316)  评论(0编辑  收藏  举报