题目:链表A,每个节点存放一个新的链表B1,B2,B3,B4,B5的头结点。场景:一个年级,相当链表A该年级5个班,每个班5个人,相当于链表B1–B5做一个学生成绩管理系统学生成绩有语文 数学 英语做这个题目的关键点在于如何把链表B放入到链表A的节点里面去。
#include<stdio.h>
#include<stdlib.h>
struct student
{
int Chinese;
int Math;
int English;
int sum;
char name[128];
struct student* next;
};
struct class
{
int class_name;
struct student* people;
struct class* next;
};
struct student* insert_student(struct student* S_head,struct student* new)
{
struct student* p=S_head;
if(p==NULL){
p=new;
return p;
}
while(p!=NULL){
if(p->next==NULL){
p->next=new;
return S_head;
}
p=p->next;
}
}
struct class* insert_class(struct class* C_head,struct class* new)
{
struct class* p=C_head;
if(p==NULL){
p=new;
return p;
}
while(p!=NULL){
if(p->next==NULL){
p->next=new;
return C_head;
}
p=p->next;
}
}
struct student* init_student(struct student* S_head)
{
struct student* new;
new=(struct student*)malloc(sizeof(struct student));
puts("请输入学生名字:");
scanf("%s",new->name);
puts("请输入语文成绩:");
scanf("%d",&new->Chinese);
puts("请输入数学成绩:");
scanf("%d",&new->Math);
puts("请输入英语成绩:");
scanf("%d",&new->English);
new->sum=new->Chinese+new->Math+new->English;
new->next=NULL;
S_head=insert_student(S_head,new);
return S_head;
}
struct class* init_class(struct class* C_head,int i)
{
struct class* new;
new=(struct class*)malloc(sizeof(struct class));
new->class_name=i+1;
for(int n=0;n<5;n++){
new->people=init_student(new->people);
}
new->next=NULL;
C_head=insert_class(C_head,new);
return C_head;
}
void print_Information(struct class* C_head)
{
struct class* p1=C_head;
struct student* p2;
while(p1!=NULL){
printf("班级%d:\n",p1->class_name);
p2=p1->people;
while(p2!=NULL){
printf("%s: 语文:%d 数学:%d 英语:%d 总分:%d\n",p2->name,p2->Chinese,p2->Math,p2->English,p2->sum);
p2=p2->next;
}
printf("\n");
p1=p1->next;
}
}
void average_score(struct class* C_head)
{
int i;
int SUM=0;
float Average;
struct class* p1=C_head;
struct student* p2;
while(p1!=NULL){
i=0;
p2=p1->people;
printf("班级%d:\n",p1->class_name);
while(p2!=NULL){
SUM=SUM+p2->sum;
i++;
p2=p2->next;
}
Average=(float)SUM/i;
printf("总分:%d, 平均分:%.2f\n",SUM,Average);
SUM=0;
p1=p1->next;
}
}
void score_Max(struct class* C_head)
{
struct class* p1=C_head;
struct student* p2;
struct student* Max;
Max=p1->people;
while(p1!=NULL){
p2=p1->people;
while(p2!=NULL){
if(Max->sum<p2->sum){
Max=p2;
}
p2=p2->next;
}
p1=p1->next;
}
printf("最高分是:%s %d\n",Max->name,Max->sum);
}
void score_Min(struct class* C_head)
{
struct class* p1=C_head;
struct student* p2;
struct student* Min;
Min=p1->people;
while(p1!=NULL){
p2=p1->people;
while(p2!=NULL){
if(Min->sum>p2->sum){
Min=p2;
}
p2=p2->next;
}
p1=p1->next;
}
printf("最低分是:%s %d\n",Min->name,Min->sum);
printf("\n");
}
int main()
{
struct class* C_head;
for(int i=0;i<5;i++){
C_head=init_class(C_head,i);
}
print_Information(C_head);
average_score(C_head);
score_Max(C_head);
score_Min(C_head);
return 0;
}
做这个题目的关键点在于如何把链表B放入到链表A的节点里面去。我们在创建A链表的结构体的时候需要创建一个指针来存放B链表的头节点,这样链表A的每个节点都有一个链表B的头节点,有了链表B的头节点,我们在遍历链表A的时候都会把每个节点里面的链表B给遍历出来。