7-1 链表也简单fina

将学生成绩绩点组成一个链表。链表结构如下:
struct student {
string name; //学生姓名
double gpa; //绩点
student *next;
};

输入是一组学生的姓名和绩点,以链表形式存储。 删除绩点小于平均绩点的学生结点,成为一个新链表。 后按照输入的顺序,依序输出新链表的学生信息。平均绩点是输入的所有学生绩点取算术平均值。

输入格式:

输入包括若干行。 每行是一个学生的 姓名和绩点,以空格隔开。
最后一行是-1。

输出格式:

输出包括学生姓名。 每个学生姓名一行。

输入样例:

zhang 3.5
liu 2.1
tie 1.9
-1

输出样例:

zhang
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu {
char name[20]; 
double s; 
struct stu *next;
};
struct stu *create();
struct stu *del(struct stu *head,double score);
double average(struct stu *head);
void printlist(struct stu *head);
int main()
{
	double s;
	struct stu *head=NULL;
	head=create();
	s=average(head);
	head=del(head,s);
	printlist(head);
	return 0;
 } 
struct stu *create()
{
	struct stu *head=NULL,*p,*tail;
	char name[20];
	while(1)
	{
        scanf("%s",name);
        if(name[0]=='-')
            break;
		p=(struct stu *)malloc(sizeof(struct stu));
		strcpy(p->name,name); 
		scanf("%lf",&p->s);
		if(head==NULL)
		{
		head=tail=p;
		}
		else
		{
		tail->next=p;
		}
		tail=p;
	}
    tail->next = NULL;
	return head;
}
struct stu *del(struct stu *head,double score)
{
	struct stu *p,*p1;
	while(head!=NULL&&head->s<score)
	{
		p=head;
		head=head->next ;
		free(p);
	}
	if(head==NULL)  return NULL;
	p=head,p1=head->next ;
	while(p1!=NULL)
	{
		if(p1->s <score)
		{
			p->next = p1->next ;
			free(p1);
		}
		else
		{
			p=p->next;
			
		}
		p1=p->next ;
	}
	return head;
}
double average(struct stu *head)
{
	double s=0,count=0;
	struct stu *p;
	for(p=head;p!=NULL;p=p->next)
	{
		s+=p->s;
		count++;
	}
	s=s/count;
	return s;
}
void printlist(struct stu *head)
{
	struct stu *p;
	for(p=head;p!=NULL;p=p->next)
	{
		printf("%s\n",p->name);
	}
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu {
char name[20]; 
double s; 
struct stu *next;
};
struct stu *del(struct stu *head,double score);
int main()
{
	double s;
	int count=0;
	struct stu *head=NULL,*p,*tail;
	char name[20];
	while(1)
	{
        scanf("%s",name);
        if(name[0]=='-')
            break;
		p=(struct stu *)malloc(sizeof(struct stu));
		strcpy(p->name,name); 
		scanf("%lf",&p->s);
		s+=p->s;
		if(head==NULL)
		{
		head=tail=p;
		}
		else
		{
		tail->next=p;
		}
		tail=p;
		count++;
	}
    tail->next = NULL;
    s=s/count;
	for(p=head;p!=NULL;p=p->next)
	{
		if(p->s>s){
				printf("%s\n",p->name);
		}
	
	}
	return 0;
 } 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mxrone

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值