BJFU-218-基于链式存储结构的图书信息表的最贵图书的查找

如果编译不通过,可以将C该为C++

#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,int n)
{
    B = (BList)malloc(sizeof(Book));
    B->next = NULL;
    BList rear = B;
    for(int i=1;i<=n;i++)
    {
       //这里用的是尾插法
       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;      
    }
}
//获得最贵图书的价格,并返回其值
double getCost(BList &B)
{
    BList p = B->next;
    double cost = p->price;
    int cout = 0;
    while(p)
    {
        if(p->price > cost)
        {
            cost = p->price;
        }
        p = p->next;
    }
    return cost;
}
void traverse(BList B,int n,double cost)
{
    BList p = B->next;
    int cout = 0;
    for(int i=1;i<=n;i++)
    {
        if(p->price==cost) cout++;     //计算最贵图书的个数
        p = p->next;
    }
    printf("%d\n",cout);

    BList q = B->next;
    for(int i=1;i<=n;i++)
    {

        if(q->price==cost)           //如果图书的价格是cost,就输出
        {
            printf("%.0f ",q->no);
            printf("%s ",q->name);
            printf("%.2f",q->price);
            printf("\n");

        }
        q = q->next;
    }


}

int main()
{
    BList B;
    int n;
    double cost;
    scanf("%d",&n);
    CreatList(B,n);
    cost = getCost(B);
    traverse(B,n,cost);
    return 0;
}

 

posted @ 2019-10-26 20:52  wwww2  阅读(863)  评论(0编辑  收藏  举报