用链表来实现学生成绩管理
目标/功能:链表A,每个节点存放一个新的链表B1,B2,B3,B4,B5的头结点。
场景:一个年级,相当链表A
该年级5个班,每个班5个人,相当于链表B1–B5
做一个学生成绩管理系统
学生成绩有语文 数学 英语
功能: 录入成绩 找三科总分的最高分 最低分 算出平均分
思路:
1、先构建一个链表(包含学生姓名、三项成绩),输入输出学生。
2、使用链表方式输入(3位)学生信息,在同时输出(定义3个链表分别将链表尾的地址指向第二个链表头,最后一个链表尾指向NULL)。
3、首先要输入班级的数量和每个班级学生的数量;根据输入变量自动添加学生信息、班级链表节点。在依次输出。
4、程序优化下,然后新增求最高分、最低分、平均分的函数等。
构建一个链表输入输出学生信息。
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 struct Student
5 {
6 char Name[10];
7 // int code;
8 int Chinese;
9 int Math;
10 int English;
11 struct Student *next;
12
13 };
14
15 int Inputscore(struct Student *head)
16 {
17 struct Student *p;
18 p = head;
19 printf("plese Input student name :");
20 scanf("%s",p->Name);
21 printf("plese Input Chinese score :");
22 scanf("%d",&p->Chinese);
23 printf("plese Input Math score :");
24 scanf("%d",&p->Math);
25 printf("plese Input English score :");
26 scanf("%d",&p->English);
27 printf("-----------------------\n");
28 return 1;
29 }
30
31 void Outputscore(struct Student *head)
32 {
33 struct Student *p;
34 p = head;
35 if(p != NULL)
36 {
37 printf("Student Name: %s\n",p->Name);
38 printf("Chinese score: %d\n",p->Chinese);
39 printf("Math score: %d\n",p->Math);
40 printf("English score: %d\n",p->English);
41 p = p->next;
42 }
43 }
44
45 int main()
46 {
47 int student_len = 3;
48 struct Student S1;
49 struct Student S2;
50 struct Student S3;
51
52 S1.next = &S2;
53 S2.next = &S3;
54 S3.next =NULL;
55 while(1){
56 if(Inputscore(&S1)){
57 Outputscore(&S1);
58 }
59 }
60
61 return 0;
62 }
63
要注意数组的首地址就是数组名 所以在scanf输入名字的时候,不需要加取地址符号(&)。
运行程序效果如下:
可以看出来,现在只能是输入一位学生的信息,然后就直接打印出来。
接下来需要输入多个学生信息(例如:3个),然后分别将学生信息依次输出。代码如下
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 struct Student
5 {
6 char Name[10];
7 // int code;
8 int Chinese;
9 int Math;
10 int English;
11 struct Student *next;
12
13 };
14
15 int Inputscore(struct Student *head)
16 {
17 struct Student *p;
18 p = head;
19 while(p != NULL)
20 {
21 printf("plese Input student name :");
22 scanf("%s",p->Name);
23 printf("plese Input Chinese score :");
24 scanf("%d",&p->Chinese);
25 printf("plese Input Math score :");
26 scanf("%d",&p->Math);
27 printf("plese Input English score :");
28 scanf("%d",&p->English);
29 printf("-----------------------\n");
30 p = p->next;
31 }
32 printf("Input END\n");
33 return 0;
34 }
35
36 int Outputscore(struct Student *head)
37 {
38 struct Student *p;
39 p = head;
40 while(p != NULL)
41 {
42 printf("Student Name: %s\n",p->Name);
43 printf("Chinese score: %d\n",p->Chinese);
44 printf("Math score: %d\n",p->Math);
45 printf("English score: %d\n",p->English);
46 printf("============\n");
47 p = p->next;
48 }
49 printf("Outout END\n");
50 return 0;
51 }
52
53 int main()
54 {
55 int student_len = 3;
56 struct Student S1;
57 struct Student S2;
58 struct Student S3;
59 struct Student S4;
60 struct Student S5;
61 S1.next = &S2;
62 S2.next = &S3;
63 S3.next =NULL;
64 while(1){
65 if(Inputscore(&S1) == 0){
66 Outputscore(&S1);
67 }
68 }
69 return 0;
70 }
71
运行程序,效果如下:
程序执行后要求输入班级数量和每个班级的学生数量,然后在依次输入每个班级的学生姓名及成绩,最后在依次打印输出。
1 #include<stdio.h>
2 #include<stdlib.h>
3 #define len 3
4
5 //#define sizeof(struct Class) Class_len
6 //#define sizeof(struct Student) Student_len
7 struct Class
8 {
9 int Class_code;
10 struct Student *head;
11 struct Class *next;
12
13 };
14
15 struct Student
16 {
17 char Name[10];
18 // int code;
19 int Chinese;
20 int Math;
21 int English;
22 struct Student *next;
23
24 };
25
26 struct Student *addStudent(struct Student *next ,struct Student *new)//尾插
27 {
28 struct Student *p;
29 p =next;
30 if(p == NULL){
31 p =new;
32 return p;
33 }
34 while(p->next !=NULL){
35 p = p->next;
36 }
37 p->next =new;
38 return p;
39 }
40
41 struct Student *Inputstudent(struct Student *head, int c_num, int s_num)
42 {
43 static int j = 1;
44 static int i = 0;
45 struct Student *p;
46 struct Student *new;
47 p = head;
48 for(i = 1; i <= s_num; i++)
49 {
50 new = (struct Student*)malloc(sizeof(struct Student));
51 printf("plese Input student name :");
52 scanf("%s",new->Name);
53 printf("plese Input Chinese score :");
54 scanf("%d",&new->Chinese);
55 printf("plese Input Math score :");
56 scanf("%d",&new->Math);
57 printf("plese Input English score :");
58 scanf("%d",&new->English);
59 printf("---Input student%d END----\n",i);
60 p =addStudent(p,new);
61 }
62 return p;
63 }
64
65 struct Class *addclass(struct Class *next ,struct Class *new)
66 {
67 struct Class *p;
68 p =next;
69 if(next == NULL){
70 next =new;
71 return next;
72 }
73 while(p->next !=NULL){
74 p = p->next;
75 }
76 p->next =new;
77 return p;
78 }
79
80 struct Class *Inputclass(struct Class *head, int c_num, int s_num)
81 {
82 int j = 1;
83 int i = 0;
84 struct Class *p;
85 struct Class *new = NULL;
86 struct Student *p2;
87 p = head;
88 for(j = 1; j <= c_num; j++)
89 {
90 new = (struct Class*)malloc(sizeof(struct Class));
91 new->Class_code = j;
92 p2 = Inputstudent(p2, c_num, s_num);
93 printf("Input class%d END\n",j);
94 new->head =p2;
95 p = addclass(p ,new);
96 }
97 printf("Input ALL DATA END\n");
98 return p;
99 }
100
101
102 void Outputscore(struct Class *head , int c_num, int s_num)
103 {
104 static int i;
105 static int j;
106 struct Class *p = NULL;
107 struct Student *p2 = NULL;
108 p =head;
109 p2 = p->head;
110 for(j = 1;j <= c_num; j++)
111 {
112 for(i = 1;i <= s_num; i++)
113 {
114 printf("Class : %d Student Name: %s\n",p->Class_code, p2->Name);
115 printf("Chinese : %d Math : %d English : %d\n", p2->Chinese, p2->Math, p2->English);
116 printf("===output student %d end===\n", i);
117 p2 = p2->next;
118 }
119 printf("output class %d end \n", j);
120 }
121 }
122
123 int main()
124 {
125 int c_num;
126 int s_num;
127 int flag = 1;
128 struct Class *C1 = NULL;
129 struct Student *S1 = NULL;
130 // C1 =(struct Class *)malloc(sizeof(struct Class));
131 // S1 =(struct Student *)malloc(sizeof(struct Student));
132 while(1){
133 printf("pleas input class number :");
134 scanf("%d",&c_num);
135 printf("pleas input student number :");
136 scanf("%d",&s_num);
137
138 C1 = Inputclass(C1, c_num, s_num);
139 Outputscore(C1 , c_num, s_num);
140
141 }
142 return 0;
143 }
144
程序执行效果如下
在链表里新增总分和平均分后,编译执行程序后出现段错误,发现是因为增加班级节点的时候,新建的学生信息节点是野指针,且没有开辟空间,所以在回调增加学生信息节点函数时出现段错误(哈,这一开始没搞懂问题出现哪里,后面自己慢慢通过printf函数作“断点”,慢慢验证过来的)。
最终代码如下:
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 struct Class
5 {
6 int Class_code;
7 struct Student *head;
8 struct Class *next;
9
10 };
11
12 struct Student
13 {
14 char Name[10];
15 int Chinese;
16 int Math;
17 int English;
18 int Sum;
19 float Avg;
20 struct Student *next;
21
22 };
23
24 struct Student *addStudent(struct Student *next ,struct Student *new);
25 struct Student *Inputstudent(struct Student *head, int c_num, int s_num);
26 struct Class *addclass(struct Class *next ,struct Class *new);
27 struct Class *Inputclass(struct Class *head, int c_num, int s_num);
28 struct Class *Inputclass(struct Class *head, int c_num, int s_num);
29 void Outputscore(struct Class *head , int c_num, int s_num);
30
31 struct Student *addStudent(struct Student *next ,struct Student *new)//尾插
32 {
33 struct Student *p;
34 p =next;
35 if(p == NULL){
36 p =new;
37 return p;
38 }
39 while(p->next !=NULL){
40 p = p->next;
41 }
42 p->next =new;
43 return p;
44 }
45
46 struct Student *Inputstudent(struct Student *head, int c_num, int s_num)
47 {
48 static int j = 1;
49 static int i = 0;
50 struct Student *p;
51 struct Student *new;
52 p = head;
53 for(i = 1; i <= s_num; i++)
54 {
55 new = (struct Student*)malloc(sizeof(struct Student));
56 printf("plese Input student name :");
57 scanf("%s",new->Name);
58 printf("plese Input Chinese score :");
59 scanf("%d",&new->Chinese);
60 printf("plese Input Math score :");
61 scanf("%d",&new->Math);
62 printf("plese Input English score :");
63 scanf("%d",&new->English);
64 printf("---Input student%d END----\n",i);
65 new->Sum = new->English + new->Chinese + new->Math;
66 new->Avg = (float) (new->Sum/3);
67 p =addStudent(p,new);
68 }
69 return p;
70 }
71
72 struct Class *addclass(struct Class *next ,struct Class *new)
73 {
74 struct Class *p;
75 p =next;
76 if(next == NULL){
77 next =new;
78 return next;
79 }
80 while(p->next !=NULL){
81 p = p->next;
82 }
83 p->next =new;
84 return p;
85 }
86
87 struct Class *Inputclass(struct Class *head, int c_num, int s_num)
88 {
89 int j = 1;
90 int i = 0;
91 struct Class *p;
92 struct Class *new = NULL;
93 struct Student *p2 = NULL;
94 p = head;
95 for(j = 1; j <= c_num; j++)
96 {
97 new = (struct Class*)malloc(sizeof(struct Class));
98 new->Class_code = j;
99 p2 = Inputstudent(p2, c_num, s_num);
100 printf("Input class%d END\n",j);
101 new->head =p2;
102 p = addclass(p ,new);
103 }
104 printf("Input ALL DATA END\n");
105 return p;
106 }
107
108 struct Student *FindMAX(struct Class *head, int course)
109 {
110 int MAX = 0;
111 char *name =NULL;
112 struct Class *p =head;
113 struct Student *p2 = NULL;
114 if(p != NULL){
115 p2 = p->head;
116 }
117 switch(course)
118 {
119 case 1:
120 if(p2 !=NULL){
121 MAX = p2->Chinese;
122 name = p2->Name;
123 }
124 while(p->next != NULL)
125 {
126 while(p2 ->next !=NULL)
127 {
128 if(MAX < p2->next->Chinese){
129 MAX = p2->next->Chinese;
130 name = p2->next->Name;
131 }
132 p2 = p2->next;
133 }
134 p = p->next;
135 p2 = p->head;
136 }
137 printf("find Chinese MAX--student name : %s score: %d \n",name ,MAX) ;
138 break;
139 case 2:
140 if(p2 != NULL){
141 MAX = p2->Math;
142 name = p2->Name;
143 }
144 while(p->next != NULL)
145 {
146 while(p2 ->next !=NULL){
147 if(MAX < p2->next->Math){
148 MAX = p2->next->Math;
149 name = p2->next->Name;
150 }
151 p2 = p2->next;
152 }
153 p = p->next;
154 p2 = p->head;
155 }
156 printf("find Math MAX--student name : %s score : %d \n",name ,MAX);
157 break;
158 case 3:
159 if(p2 != NULL){
160 MAX = p2->English;
161 name = p2->Name;
162 }
163 while(p->next != NULL)
164 {
165 while(p2 ->next !=NULL){
166 if(MAX < p2->next->English){
167 MAX = p2->next->English;
168 name = p2->next->Name;
169 }
170 p2 = p2->next;
171 }
172 p = p->next;
173 p2 = p->head;
174 }
175 printf("find English MAX--student name : %s score : %d \n ",name ,MAX );
176 break;
177 case 0:
178 break;
179
180 }
181 return p2;
182 }
183
184 struct Student *PrintfMIN(struct Class *head )
185 {
186 int Chinese_MIN = 0;
187 int Math_MIN = 0;
188 int English_MIN = 0;
189 char *name1 =NULL;
190 char *name2 =NULL;
191 char *name3 =NULL;
192 struct Class *p =head;
193 struct Student *p2 = NULL;
194 if(p != NULL){
195 p2 = p->head;
196 }
197 if(p2 !=NULL){
198 Chinese_MIN = p2->Chinese;
199 Math_MIN = p2->Math;
200 English_MIN = p2->English;
201 name1 = p2->Name;
202 name2 = p2->Name;
203 name3 = p2->Name;
204 }
205 while(p->next != NULL)
206 {
207 while(p2 ->next !=NULL)
208 {
209 if(Chinese_MIN > p2->next->Chinese){
210 Chinese_MIN = p2->next->Chinese;
211 name1 = p2->next->Name;
212 }
213 if(Math_MIN > p2->next->Math){
214 Math_MIN = p2->next->Math;
215 name2 = p2->next->Name;
216 }
217 if(English_MIN > p2->next->English){
218 English_MIN = p2->next->English;
219 name3 = p2->next->Name;
220 }
221 p2 = p2->next;
222 }
223 p = p->next;
224 p2 = p->head;
225 }
226 printf("find Chinese MIN--student name : %s score: %d \n",name1 ,Chinese_MIN);
227 printf("find Math MAX--student name : %s score: %d \n",name2 ,Math_MIN);
228 printf("find English MAX--student name : %s score: %d \n",name3 ,English_MIN);
229 return p2;
230 }
231
232 void Outputscore(struct Class *head , int c_num, int s_num)
233 {
234 static int i;
235 static int j;
236 struct Class *p = NULL;
237 struct Student *p2 = NULL;
238 p =head;
239 p2 = p->head;
240 for(j = 1;j <= c_num; j++)
241 {
242 for(i = 1;i <= s_num; i++)
243 {
244 printf("Class : %d Student Name: %s\n",p->Class_code, p2->Name);
245 printf("Chinese : %d Math : %d English : %d Sum : %d Avg : %f\n", p2- >Chinese, p2->Math, p2->English, p2->Sum, p2->Avg);
246 printf("===output student %d end===\n", i);
247 p2 = p2->next;
248 }
249 printf("output class %d end \n", j);
250 }
251 }
252
253 int main()
254 {
255 int c_num;
256 int s_num;
257 int course;
258 struct Class *C1 = NULL;
259 while(1){
260 printf("pleas input class number :");
261 scanf("%d",&c_num);
262 printf("pleas input student number :");
263 scanf("%d",&s_num);
264 C1 = Inputclass(C1, c_num, s_num);
265 Outputscore(C1 , c_num, s_num);
266 printf("pleas input find course :");
267 scanf("%d",&course);
268 FindMAX(C1, course);
269 PrintfMIN(C1);
270 return 0;
271 }
272 }
效果如下:
花费了大约8天时间修改该代码,之前还吧结构体和链表的概念弄混了,复习了一遍视频才巩固好。代码这东西还是得经常敲才行,通过输出去不断验证修改。不然真忘了,学习是一个坚持的过程。
加油,不能放弃。这句话写给大家,也是写给自己,互勉。
以上内容如有错误,望大佬指正。