#include<stdlib.h>
#include<stdio.h>
#include<string.h>
struct cla
{
int num;
int numstu;
struct cla *next;
struct stu *nextstu;
};
struct stu
{
int num;
char name[4];
int math;
int china;
int eng;
int sum;
struct stu *next;
};
int printClass(struct cla* head)
{
struct cla *p=head;
printf("开始遍历班级");
while(p!=NULL)
{
printf("%d ",p->num);
p = p->next;
}
printf("\n");
return 0;
}
int printStudent(struct stu* head)
{
struct stu *p=head;
printf("开始遍历学生");
while(p!=NULL)
{
printf("%d ",p->num);
p = p->next;
}
printf("\n");
return 0;
}
struct cla* insertClassBehind(struct cla *head,struct cla *newlink)
{
struct cla *p=head;
if(head==NULL)
{
head=newlink;
}
else
{
while(p->next!=NULL)
{
p=p->next;
}
p->next=newlink;
}
return head;
}
struct stu* insertClassStuBehind(struct stu *head,struct stu *newlink)
{
struct stu *p=head;
if(head==NULL)
{
head=newlink;
}
else
{
while(p->next!=NULL)
{
p=p->next;
}
p->next=newlink;
}
return head;
}
struct stu* cteateStudentinkBehind(struct stu *head)
{ struct stu *p=head;
struct stu *newlink;
for(int i=0;i<5;i++)
{
newlink=(struct stu*)malloc(sizeof(struct stu));
newlink->next=NULL;
printf("开始创建%d号同学\n",i+1);
newlink->num=i+1;
newlink->china=95+i;
newlink->math=95+i;
newlink->eng=95+i;
newlink->sum=newlink->china+newlink->math+newlink->eng;
head=insertClassStuBehind(head,newlink);
printStudent(head);
}
return head;
printf("创建该班级学生结束\n");
}
struct cla* cteateClassLinkBehind(struct cla *head)
{ struct cla *p=head;
struct cla *newlink;
for(int i=0;i<5;i++)
{
newlink=(struct cla*)malloc(sizeof(struct cla));
newlink->next=NULL;
printf("开始创建%d班\n",i+1);
newlink->num=i+1;
head=insertClassBehind(head,newlink);
}
return head;
printf("创建班级结束\n");
}
struct cla * connectClassStudentLink(struct cla *head,struct stu *headstu)
{
struct cla *p=head;
struct stu *s=headstu;
while(head!=NULL)
{
head->nextstu=cteateStudentinkBehind(headstu);
head=head->next;
}
return p;
}
int printClassStudent(struct cla *head,struct stu *headstu)
{
struct cla *p=head;
struct stu *s=head->nextstu;
while(p!=NULL)
{
printf("%d班 ",p->num);
s=p->nextstu;
while(s!=NULL)
{
printf("%d号 ",s->num);
s =s ->next;
}
p = p->next;
printf("\n");
}
}
int printClassStudentScore(struct cla *head,struct stu *headstu)
{
struct cla *p=head;
struct stu *s=head->nextstu;
int sumscore=0;
int avg;
int countstudent=0;
while(p!=NULL)
{
printf("%d班 ",p->num);
printf("\n");
s=p->nextstu;
while(s!=NULL)
{
printf("%d号 ",s->num);
printf("总分%d \n",s->sum);
sumscore=sumscore+s->sum;
countstudent++;
s =s ->next;
}
p = p->next;
printf("\n");
}
avg=sumscore/countstudent;
printf("平均总分:%d\n累加总分:%d\n学生人数:%d\n",avg,sumscore,countstudent);
}
int main()
{
struct cla *head=NULL;
struct stu *headstu=NULL;
head=cteateClassLinkBehind(head);
head->nextstu=headstu;
// headstu=cteateStudentinkBehind(headstu);
// printStudent(headstu);
head=connectClassStudentLink(head,headstu);
printClassStudent(head,headstu);
printClassStudentScore(head,headstu);
}