BJFU-215-基于链式存储结构的图书信息表的排序

这里用的是冒泡排序

#include<stdio.h>
#include<stdlib.h>
#define MAX 100

typedef struct Book{
    double no;
    char name[MAX];
    double price;
    struct Book * next;
}Book,*BList;

void CreatList(BList &B)
{
    B = (BList)malloc(sizeof(Book));
    B->next = NULL;
    BList rear = B;
    while(1)
    {
       BList p = (BList)malloc(sizeof(Book));
       scanf("%lf",&p->no);
       scanf("%s",p->name);
       scanf("%lf",&p->price);
       if(p->no==0&&p->name[0]=='0'&&p->price==0) break;
       rear->next = p;
       p->next = NULL;
       rear = p;

    }
}

void traverse(BList B)
{
    BList p = B->next;
    while(p)
    {
        printf("%.0f ",p->no);
        printf("%s ",p->name);
        printf("%.2f",p->price);
        printf("
");
        p = p->next;
    }
}
int getLen(BList B)
{
    int i=0;
    BList p = B->next;
    while(p)
    {
        p = p->next;
        i++;
    }
    return i;

}
//冒泡排序
void SortList(BList &B)
{
    BList p,pre,q,last;
    double t;
    for(int i=0;i<getLen(B)-1;i++)
    {
        p = B;
        for(int j=0;j<getLen(B)-i-1;j++)
        {
            pre = p;
            q = p->next;
            if(q->price < q->next->price)
            {
                last = q->next;
                pre->next = last;
                q->next = last->next;
                last->next = q;
            }
            p = p->next;
        }
    }

}

int main()
{
    BList B;
    CreatList(B);
    SortList(B);
    traverse(B);
    return 0;
}
原文地址:https://www.cnblogs.com/wwww2/p/11745552.html