THE_BUG_ 2020-04-29 11:11 采纳率: 0%
浏览 844

C语言链表查找最贵图书?

40分) 输出最贵的书籍信息:
结构体类型定义如下:
struct stu{
char name[20];
float price;
struct stu *next;
};
要求:
(1)编写自定义函数创建链表,输入书名和价格。输入Y继续创建,输入其他字符结束创建。
(2)编写自定义函数输出最贵的书籍信息。
(3)编写自定义函数释放链表内存。

**输入提示:"请输入书名 价格: \n"(循环)
**输入格式:"%s %f"
**是否继续创建的提示:"是否继续输入,按Y键继续输入,其他键就结束.\n"
**输出提示:"result:\n"
**输出格式:"%s %.2f\n"

程序运行示例:
请输入书名 价格: ↙
Algorithms 105
是否继续输入,按Y键继续输入,其他键就结束.↙
Y
请输入书名 价格: ↙
高等数学 31.5
是否继续输入,按Y键继续输入,其他键就结束.↙
Y
请输入书名 价格: ↙
C语言 35
是否继续输入,按Y键继续输入,其他键就结束.↙
n↙
result:↙
Algorithms 105.00↙

  • 写回答

2条回答 默认 最新

  • 一闪接大 2021-05-31 19:59
    关注

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    struct stu *app(struct stu *head);
    float dis(struct stu *head, char *p);
    void dele(struct stu *head);
    struct stu{
        char name[20];
        float price;
        struct stu *next;
    };
    int main(void)
    {

        char c='Y';
        char name[20];
        float maxprice;
        struct stu *head =NULL;

        while (c=='Y')
        {
            head = app(head);

            printf("是否继续输入,按Y键继续输入,其他键就结束.\n");
            scanf(" %c", &c);
        }
        maxprice=dis(head,name);
        printf("result:\n");
        printf("%s %.2f\n",name,maxprice );
        dele(head);
        return 0;
    }
    struct stu *app(struct stu *head)
    {
        struct stu *p=NULL,*pr=head;
        char name[20];
        float price;
        p=(struct stu *)malloc(sizeof(struct stu));
        if (p==NULL)
        {
            exit(0);
        }
        if (head == NULL)
        {
            head=p;
        }
        else
        {
            while (pr->next != NULL)
            {
                pr=pr->next;
            }
            pr->next=p;
        }
        printf("请输入书名 价格: \n");
        scanf("%s %f",name,&price);
        strcpy(p->name,name);
        p->price=price;
        p->next=NULL;
        return head;
    }

    float dis(struct stu *head, char *p)
    {
        struct stu *pq=head;
        float j=0;

        while (pq!=NULL)
        {
            if (pq->price>j)
                {
                    j=pq->price;
                    strcpy(p,pq->name);
                }
            pq=pq->next;
        }
        return j;
    }


    void dele(struct stu *head)
    {
        struct stu *p=head , *pr = NULL;
        while (p!=NULL)
        {
            pr=p;
            p=p->next;
            free(pr);
        }
    }
     

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?