c语言:(复杂的0.0)单向链表排序

毫无意义的一遍bk,可能这东西以后有用?
(又复杂又佣长)

一道为了链表而链表的c语言题目
cc-test08-1链表的输入与输出

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct student
{
    int num;
    char name[25];
    char xb;
    double score[5];
    double ave;
    double sum;
    struct student* next;
};
void swap1(struct student *x,struct student *p)
{
    struct student t;
    t=*x;
    struct student *s1,*s2;
    s1=x->next;
    s2=p->next;
    *x=*p;
    x->next=s1;
    *p=t;
    p->next=s2;
}
void sort1(struct student *head)
{
    struct student *p=head;
    while(p!=NULL)
    {
        struct student *x=p->next;
        while(x!=NULL)
        {
            if(x->sum>p->sum)
            {
                swap1(x,p);
            }
            x=x->next;
        }
        p=p->next;
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    struct student *head,*p1,*p2;
    p1=(struct student *)malloc(sizeof(struct student));
    head=p1;
    scanf("%d%*c%[^\n]%*c%c%lf%lf%lf",&p1->num,p1->name,&p1->xb,&p1->score[1],&p1->score[2],&p1->score[3]);
    p1->sum=p1->score[1]+p1->score[2]+p1->score[3];
    p1->ave=p1->sum/3;
    while(--n)
    {
        p2=(struct student *)malloc(sizeof(struct student));
        p1->next=p2;
        scanf("%d%*c%[^\n]%*c%c%lf%lf%lf",&p2->num,p2->name,&p2->xb,&p2->score[1],&p2->score[2],&p2->score[3]);
        p2->sum=p2->score[1]+p2->score[2]+p2->score[3];
        p2->ave=p2->sum/3;
        p2->next=NULL;
        p1=p2;
    }
    sort1(head);
    p1=head;
    while(p1!=NULL)
    {
        printf("%d  %s  %c  %.2lf  %.2lf  %.2lf  %.2lf  %.2lf\n",p1->num,p1->name,p1->xb,p1->score[1],p1->score[2],p1->score[3],p1->ave,p1->sum);
        p1=p1->next;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值