zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    十大开源CRM
    EL表达式【转】
    zk调用js(转)
    MVC4学习笔记(三) 数据验证设计
    MVC4学习笔记(一) 认识MVC
    MVC4学习笔记(四) MVC界面设计
    hibernate介绍
    如何在oracle中导入dmp数据库文件
    JS的delete操作
    使用JavaScript给Html元素加边框
  • 原文地址:https://www.cnblogs.com/wwww2/p/11745552.html
Copyright © 2011-2022 走看看