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

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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题