#include<stdio.h>
#include<stdlib.h>
#define N 2
struct stu
{
int num;
char name[10];
int gra;
struct student *next;
};
int main()
{
struct stu *creat(struct stu *head,int n);
struct stu *findmax(struct stu *head);
struct stu *dele(struct stu *head,int x);
void print(struct stu *head);
int max;
struct stu *head=NULL;
head=creat(head,N);
print(head);
max=findmax(head);
head=dele(head,max);
printf("删除后");
print(head);
return 0;
}
struct stu *creat(struct stu *head,int n)
{
struct stu *p,*q;
int i;
for(i=0;i<n;i++)
{
q=(struct stu * )malloc(sizeof(struct stu));
printf("请输入第%d名学生的学号姓名和成绩\n",i+1);
scanf("%d%s%d",&q->num,&q->name,&q->gra);
q->next=NULL;
if(head==NULL)
head=q;
else
p->next=q;
p=q;
}
return head;
}
void print(struct stu *head)
{
struct stu *p=head;
printf("学生信息为\n");
while(p!=NULL)
{
printf("%d %s %d\n",p->num,p->name,p->gra);
p=p->next;
}
}
struct stu *dele(struct stu *head,int x)
{
struct stu *p,*q;
p=head;
while(p!=NULL&&p->gra!=x)
{
q=p;
p=p->next;
}
if(p==head)
head=p->next;
else
q->next=p->next;
free(p);
return head;
}
struct stu *findmax(struct stu *head)
{
struct stu *p;
int max=0;
p=head;
while(p!=NULL)
{
if(p->gra>max)
{
max=p->gra;
}
p=p->next;
}
printf("最高成绩为%d\n",max);
return max;
}
结构体记录学生信息并找出最高成绩并删除后输出
最新推荐文章于 2022-11-15 22:40:51 发布