链表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;
}