Using linked list to realize student achievement management
Objective / function: linked list A, each node stores A new head node of linked list B1, B2, B3, B4 and B5.
Scene: A grade, equivalent to A linked list
There are 5 classes in this grade, with 5 people in each class, which is equivalent to the linked list B1 – B5
Do a student achievement management system
Students' grades are Chinese, mathematics and English
Function: enter the score, find the highest score and the lowest score of the three subjects, and calculate the average score
Idea:
1. First build a linked list (including student names and three grades) and input and output students.
2. Use the linked list method to input (3-bit) student information and output it at the same time (define 3 linked lists, respectively point the address of the end of the linked list to the second chain header and the last end of the linked list to NULL).
3. First, input the number of classes and the number of students in each class; Automatically add student information and class linked list nodes according to input variables. Output in sequence.
4. Program optimization, and then add the function to calculate the highest score, lowest score and average score.
Build a linked list to input and output student information.
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
Note that the first address of the array is the array name, so when you enter the name in scanf, you don't need to add the address symbol (&).
The effect of running the program is as follows:
You can see that now you can only enter the information of one student and then print it directly.
Next, you need to input multiple student information (for example, 3), and then output the student information in turn. The code is as follows
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
Run the program, and the effect is as follows:
After the program is executed, it is required to input the number of classes and the number of students in each class, then input the student name and score of each class in turn, and finally print out in turn.
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)//Tail insertion 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
The program execution effect is as follows
After adding the total score and average score in the linked list, there was a segment error after compiling the execution program. It was found that when adding the class node, the new student information node was a wild pointer and did not open up space, so there was a segment error when calling back to add the student information node function (HA, I didn't understand where the problem occurred at the beginning, and then I slowly made it through the printf function "Breakpoint", slowly verified).
The final code is as follows:
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)//Tail insertion 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 }
The effects are as follows:
It took about 8 days to modify the code. I confused the concept of structure and linked list before. I reviewed the video to consolidate it. The code still needs to be typed frequently and verified and modified continuously through output. Otherwise, I really forget that learning is a persistent process.
Come on, don't give up. This sentence is written not only for everyone, but also for yourself and encourage each other.
If there is any mistake in the above content, I hope the boss can correct it.