结构体记录学生信息并找出最高成绩并删除后输出

#include
#include
#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;inum,&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;
}

你可能感兴趣的:(C和C++,c语言)