鏈表A,每個節點存放一個新的鏈表B1,B2,B3,B4,B5的頭結點。
場景:一個年級,相當鏈表A
該年級5個班,每個班5個人,相當于鏈表B1–B5
做一個學生成績管理系統
學生成績有語文 數學 英語
功能: 錄入成績 找最三科總分的最高分 最低分 算出平均分
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct people
{char* name;int lesson;int number;int math;int chinese;int english;int all;struct people* next;
};struct class
{int class;struct people* firstpeople;struct class* next;
};struct class* insertfromclassbehind(struct class* classhead,struct class* classnewnode)
{struct class* p=NULL;p=classhead;if(classhead==NULL){classhead=classnewnode;return classhead;}struct people* insertfrompeoplebefore(struct people* peoplehead,struct people* peoplenewnode)
{if(peoplehead==NULL){peoplehead=peoplenewnode;}else{peoplenewnode->next=peoplehead;peoplehead=peoplenewnode;}return peoplehead;
}void linkPrintf(struct class* head)
{struct people* p=NULL;if(head==NULL){printf("打印失敗,鏈表為空\n");}while(head!=NULL){p=head->firstpeople;while(p!=NULL){printf("姓名:%s\n",p->name);printf("班級:%d\n",p->lesson);printf("學號:%d\n",p->number);printf("數學:%d\n",p->math);printf("語文:%d\n",p->chinese);printf("英語:%d\n",p->english);printf("---------------------------------------------------------------------------------------------------\n");p=p->next;}head=head->next;}
}struct class* creatnewlink(struct class* classhead,struct people* peoplehead,int classall)
{struct class* classnewnode=NULL;struct people* peoplenewnode=NULL;while(classall){int number;classnewnode=(struct class*)malloc(sizeof(struct class));classnewnode->next=NULL;classnewnode->firstpeople=NULL;printf("請輸入班級:\n");scanf("%d",&classnewnode->class);printf("請輸入該班的人數:\n");scanf("%d",&number);while(number){peoplenewnode=(struct people*)malloc(sizeof(struct people));peoplenewnode->next=NULL;peoplenewnode->name=(char*)malloc(128);memset(peoplenewnode->name,'\0',128);peoplenewnode->lesson=classnewnode->class;printf("請輸入姓名:\n");scanf("%s",peoplenewnode->name);
// printf("輸入的是:%s\n",peoplenewnode->name);printf("請輸入學號:\n");scanf("%d",&peoplenewnode->number);printf("請輸入數學成績:\n");scanf("%d",&peoplenewnode->math);printf("請輸入語文成績:\n");scanf("%d",&peoplenewnode->chinese);printf("請輸入英語成績:\n");scanf("%d",&peoplenewnode->english);peoplenewnode->all=peoplenewnode->english+peoplenewnode->math+peoplenewnode->chinese;peoplehead=insertfrompeoplebefore(peoplehead,peoplenewnode);number--;}classnewnode->firstpeople=peoplehead;peoplehead=NULL;//printf("****************************");classhead=insertfromclassbehind(classhead,classnewnode);classall--;}return classhead;
}void findmaxall(struct class*head)
{struct class* p=head;struct people* p2=p->firstpeople;struct people* max=NULL;max=p2;if(p==NULL){printf("參數不能為空!\n");}while(p!=NULL){p2=p->firstpeople;while(p2!=NULL){if(max->all<=p2->all){max=p2;}p2=p2->next;}p=p->next;}printf("---------------------------------------------------------------------------------------------------\n");printf("總分最高為:%d,姓名:%s,班級:%d,學號:%d\n",max->all,max->name,max->lesson,max->number);printf("---------------------------------------------------------------------------------------------------\n");
}void findminall(struct class*head)
{struct class* p=head;struct people* p2=p->firstpeople;struct people* min=NULL;min=p2;if(p==NULL){printf("參數不能為空!\n");}while(p!=NULL){p2=p->firstpeople;while(p2!=NULL){if(min->all>=p2->all){min=p2;}p2=p2->next;}p=p->next;}printf("總分最低為:%d,姓名:%s,班級:%d,學號:%d\n",min->all,min->name,min->lesson,min->number);printf("---------------------------------------------------------------------------------------------------\n");
}void findaverage(struct class* head)
{int mathall,chineseall,englishall,peopleall;mathall=chineseall=englishall=peopleall=0;struct people* p;if(head==NULL){printf("鏈表為空錯誤\n");}while(head!=NULL){p=head->firstpeople;while(p!=NULL){mathall=p->math+mathall;chineseall=p->chinese+chineseall;englishall=p->english+englishall;peopleall++;p=p->next;}head=head->next;}//printf("語文:%d,數學:%d,英語:%d\n",chineseall,mathall,englishall);printf("語文平均分:%f\n",(float)chineseall/peopleall);printf("數學平均分:%f\n",(float)mathall/peopleall);printf("英語平均分:%f\n",(float)englishall/peopleall);printf("---------------------------------------------------------------------------------------------------\n");
}
int main()
{struct class* classhead=NULL;struct people* peoplehead=NULL;int classall;printf("請輸入班級總數:\n");scanf("%d",&classall);classhead=creatnewlink(classhead,peoplehead,classall);findmaxall(classhead);findminall(classhead);findaverage(classhead);//linkPrintf(classhead);return 0;
}