//建立链表,求平均分,求平均分最高的学生并输出信息 ,删除学号为102学生的信息
#include "stdio.h"
#include "stdlib.h"
//定义一个结构体,储存学生信息
struct Student{
int num;
char name[20];
double score[3];
double avg;
};
//定义节点
struct Node{
struct Student stu;//数据域
struct Node *next;//指针域
};
//函数的声明:
struct Node* Avg(struct Node* p);
struct Node* Max(struct Node *p);
struct Node* Dele(struct Node *p,int site);
int main(){
int i;
struct Node *n,*p,*max,*head=NULL;
n=(struct Node*)malloc(sizeof(struct Node));//开辟一块新的空间,使 n指向他
//输入第一个学生的信息
scanf("%d%s%lf%lf%lf",&n->stu.num,n->stu.name,&n->stu.score[0],&n->stu.score[1],&n->stu.score[2]);
n->next=NULL;
p=n;// 可将 p看作是 n的小跟班 ,在指针 n指向新开辟的空间时 ,将 n的地址赋给 p
head=n;//将 head作为链表的头,固定不变,以免丢失链表
//输入之后的学生的信息
for(i=0;i<3;i++){
n=(struct Node*)malloc(sizeof(struct Node));//开辟新的空间
scanf("%d%s%lf%lf%lf",&n->stu.num,n->stu.name,&n->stu.score[0],&n->stu.score[1],&n->stu.score[2]);
n->next=NULL;
p->next=n;//将开辟的空间链接起来 ,形成链表
p=n;
}
/* for(p=Avg(head);p!=NULL;p=p->next){
printf("%d %lf\n",p->stu.num,p->stu.avg);
}
max=Max(head);
//输出最高成绩的学生的信息
printf("%d%s%lf",max->stu.num,max->stu.name,max->stu.avg);
for(i=0;i<3;i++){
printf("%lf",max->stu.score[i]);
}*/
printf("\n");
p=Avg(head);
p=Dele(p,102);
for(;p!=NULL;p=p->next){
printf("学号:%d 姓名:%s 平均分:%lf\n",p->stu.num,p->stu.name,p->stu.avg);
}
return 0;
}
//求平均值的函数 ,返回链头
struct Node* Avg(struct Node* p){
int i;
double sum;
struct Node *q=p;
for(;p!=NULL;p=p->next){
sum=0;
for(i=0;i<3;i++){
sum+=p->stu.score[i];//将每一个学生的分数都加起来
}
p->stu.avg=sum/3;
}
return q;
}
//找出分数最高的学生 ,即遍历找最值 (黑瞎子掰玉米),先固定一个值,将其与其他值比较
struct Node *Max(struct Node *p){
struct Node *max;
max=p;//将链表的头赋给max,将其表示为最大值进行比较
for(;p!=NULL;p=p->next){
if(max->stu.avg<p->stu.avg)
max=p;
}
return max;
}
struct Node* Dele(struct Node *p,int site){
int i=1,j;
struct Node *q=p;
for(;p!=NULL;p=p->next,i++){
if(p->stu.num==site)
break;
}
if(i==1){
return p->next;
}else{
for(j=1,p=q;p!=NULL;p=p->next,j++){
if(j==i-1){
p->next=p->next->next;
break;
}
}
return q;
}
}
删除102学生的信息
最新推荐文章于 2023-09-10 17:39:07 发布
905

被折叠的 条评论
为什么被折叠?



