链表A,每个节点存放一个新的链表B1,B2,B3,B4,B5的头结点。
场景:一个年级,相当链表A,该年级5个班,每个班5个人,相当于链表B1–B5
做一个学生成绩管理系统学生成绩有语文 数学 英语
功能: 录入成绩 找出三科总分的最高分 最低分 算出平均分
#include<stdio.h>
#include<stdlib.h>
struct stu{
int chinese;
int math;
int english;
int num;
struct stu *next;
};
struct class{
struct stu *head;
struct class *next;
};
struct stu *head=NULL;
struct class *chead=NULL;
struct stu* addstu(struct stu *new)
{
struct stu *p=head;
if(p==NULL){
head=new;
return head;
}
while(p->next!=NULL){
p=p->next;
}
p->next=new;
return head;
}
struct stu* initstu()
{
int i;
for(i=0;i<2;i++){
struct stu *new=(struct stu *)malloc(sizeof(struct stu));
printf("input %d chinese,math,english,num\n",i);
scanf("%d%d%d%d",&(new->chinese),&(new->math),&(new->english),&(new->num));
new->next=NULL;
head=addstu(new);
}
return head;
}
void print()
{
int i;
int j;
struct stu *p=head;
struct class *p2=chead;
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("chinese=%d,math=%d,english=%d,num=%d\n",p->chinese,p->math,p->english,p->num);
printf("sum=%d\n",(p->chinese)+(p->math)+(p->english));
p=p->next;
}
p2=p2->next;
}
}
struct class* addc(struct class *new)
{
struct class *p=chead;
while(chead==NULL){
chead=new;
return chead;
}
while(p->next!=NULL){
p=p->next;
}
p->next=new;
return chead;
}
struct class* initclass()
{
int i;
for(i=0;i<2;i++){
struct class *new;
new=(struct class *)malloc(sizeof(struct class));
new->head=initstu();
new->next=NULL;
chead=addc(new);
}
return chead;
}
void max()
{
int max;
struct stu *p=head;
struct class *p1=chead;
max=((p->chinese)+(p->math)+(p->english));
while(p1!=NULL){
while(p!=NULL){
if(max<((p->chinese)+(p->math)+(p->english))){
max=((p->chinese)+(p->math)+(p->english));
}
p=p->next;
}
p1=p1->next;
}
printf("max=%d\n",max);
}
void min()
{
int min;
struct stu *p=head;
struct class *p1=chead;
min=((p->chinese)+(p->math)+(p->english));
while(p1!=NULL){
while(p!=NULL){
if(min>((p->chinese)+(p->math)+(p->english))){
min=((p->chinese)+(p->math)+(p->english));
}
p=p->next;
}
p1=p1->next;
}
printf("min=%d\n",min);
}
void average()
{
int sum=0;;
struct stu *p=head;
struct class *p1=chead;
while(p1!=NULL){
while(p!=NULL){
sum+=((p->chinese)+(p->math)+(p->english));
p=p->next;
}
p1=p1->next;
}
printf("average=%lf\n",(float)sum/4);
}
int main()
{
initclass();
printf("--------------------------------------\n");
print();
printf("--------------------------------------\n");
max();
printf("--------------------------------------\n");
min();
printf("--------------------------------------\n");
average();
printf("--------------------------------------\n");
return 0;
}