链表A,每个节点存放一个新的链表B1,B2,B3,B4,B5的头结点。
场景:一个年级,相当链表A
该年级5个班,每个班5个人,相当于链表B1–B5
做一个学生成绩管理系统
学生成绩有语文 数学 英语
功能: 录入成绩 找最三科总分的最高分 最低分 算出平均分
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char name[100];
int china;
int math;
int english;
int sum;
float average;
struct student *next;
};
struct class
{
int num;
struct student *msg;
struct class *next;
};
struct class *addClass(struct class *head,struct class *new)
{
if(head == NULL){
head = new;
return head;
}
struct class *p = head;
while(p->next != NULL){
p=p->next;
}
p->next = new;
return head;
}
struct student *addStu(struct student *head,struct student *new)
{
if(head == NULL){
head = new;
return head;
}
struct student *p = head;
while(p->next != NULL){
p=p->next;
}
p->next = new;
return head;
}
struct student *initStu(struct student *head)
{
struct student *new = NULL;
int i;
for(i=1;i<5;i++){
new = (struct student *)malloc(sizeof(struct student));
new->next = NULL;
printf("NO.%d student name:",i);
memset(new,'\0',sizeof(struct student));
scanf("%s",new->name);
printf("chian score:");
scanf("%d",&(new->china));
printf("math score:");
scanf("%d",&(new->math));
printf("english score:");
scanf("%d",&(new->english));
new->sum = new->math + new->china + new->english;
new->average = (float)new->sum / 3;
head = addStu(head,new);
}
return head;
}
struct class *initClass(struct class *phead)
{
int i;
struct class *pnew = NULL;
struct student *head = NULL;
for(i=1;i<5;i++){
pnew = (struct class *)malloc(sizeof(struct class));
pnew->num = i;
printf("Class.%d msg\n",pnew->num);
pnew->next = NULL;
pnew->msg = initStu(NULL);
phead = addClass(phead,pnew);
}
return phead;
}
void printInfo(struct class *head)
{
struct class *p = head;
struct student *p1 = NULL;
while(p!=NULL){
printf("=============Class.%d msg...===========\n",p->num);
p1 = p->msg;
while(p1!=NULL){
printf("Stu_name:%s chian:%d math:%d english:%d sum:%d average:%f\n",p1->name,p1->china,p1->math,p1->english,p1->sum,p1->average);
p1=p1->next;
}
p=p->next;
}
}
void max_stu(struct class *head)
{
struct class *p = head;
struct student *p1 = NULL;
struct student *max = NULL;
while(p!=NULL){
p1 = p->msg;
max = p1;
while(p1!=NULL){
if((max->sum) < (p1->sum)){
max = p1;
}
p1=p1->next;
}
p=p->next;
}
printf("全段最低分是%s,语文:%d, 数学:%d 英语:%d 总分:%d\n",max->name,max->china,max->math,max->english,max->sum);
}
void min_stu(struct class *head)
{
struct class *p = head;
struct student *p1 = NULL;
struct student *min = NULL;
while(p!=NULL){
p1 = p->msg;
min = p1;
while(p1!=NULL){
if((min->sum) > (p1->sum)){
min = p1;
}
p1=p1->next;
}
p=p->next;
}
printf("全段最高分是%s,语文:%d, 数学:%d 英语:%d 总分:%d\n",min->name,min->china,min->math,min->english,min->sum);
}
int main()
{
struct class *phead = NULL;
phead = initClass(phead);
printInfo(phead);
printf("\n");
max_stu(phead);
printf("\n");
min_stu(phead);
return 0;
}