m0_65671109 2022-12-30 14:12 采纳率: 50%
浏览 85
已结题

关于#链表#的问题,如何解决?

以单链表结构表示某市场家电部的库存模型,当有提货或进货时需要对该链表及时进行维护。
(1)链表结构的数据域包括家电名称、规格型号、单价和数量,以单价体现链表的有序性;
(2)程序功能包括:初始化、创建表、排序、插入、删除、查询等。
(3)以顺序表作为存储结构,实现上述所有操作。

  • 写回答

5条回答 默认 最新

  • |__WhoAmI__| 2022-12-30 14:20
    关注
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // 定义家电信息结构体
    typedef struct
    {
        char name[30];  // 家电名称
        char model[30]; // 型号
        float price;    // 单价
        int quantity;   // 数量
    } Appliance;
    
    // 定义单链表节点结构体
    typedef struct Node
    {
        Appliance data;    // 家电信息
        struct Node *next; // 指向下一个节点的指针
    } Node;
    
    // 定义单链表结构体
    typedef struct
    {
        Node *head; // 头指针
        int size;   // 节点数量
    } LinkedList;
    
    // 初始化单链表
    void init(LinkedList *list)
    {
        list->head = NULL;
        list->size = 0;
    }
    
    // 创建一个新的单链表节点
    Node *create_node(Appliance appliance)
    {
        Node *node = (Node *)malloc(sizeof(Node));
        node->data = appliance;
        node->next = NULL;
        return node;
    }
    
    // 将一个新节点插入到单链表中
    void insert(LinkedList *list, Appliance appliance)
    {
        Node *node = create_node(appliance);
    
        // 如果单链表为空,则将新节点作为第一个节点
        if (list->size == 0)
        {
            list->head = node;
        }
        // 否则,将新节点插入到单链表的最后一个节点后面
        else
        {
            Node *current = list->head;
            while (current->next != NULL)
            {
                current = current->next;
            }
            current->next = node;
        }
    
        list->size++;
    }
    
    // 删除单链表中的一个节点
    void delete(LinkedList *list, Appliance appliance)
    {
        void delete (LinkedList * list, Appliance appliance)
        {
            Node *current = list->head;
            Node *prev = NULL;
    
            // 遍历单链表,找到要删除的节点
            while (current != NULL)
            {
                if (strcmp(current->data.name, appliance.name) == 0 &&
                    strcmp(current->data.model, appliance.model) == 0 &&
                    current->data.price == appliance.price &&
                    current->data.quantity == appliance.quantity)
                {
                    break;
                }
                prev = current;
                current = current->next;
            }
    
            // 如果找到了要删除的节点
            if (current != NULL)
            {
                // 如果要删除的节点是第一个节点
                if (prev == NULL)
                {
                    list->head = current->next;
                }
                // 否则,更新前一个节点的 next 指针,跳过当前节点
                else
                {
                    prev->next = current->next;
                }
                free(current);
                list->size--;
            }
        }
    
        // 查询单链表中是否存在指定家电
        int search(LinkedList * list, Appliance appliance)
        {
            Node *current = list->head;
    
            // 遍历单链表,查找指定家电
            while (current != NULL)
            {
                if (strcmp(current->data.name, appliance.name) == 0 &&
                    strcmp(current->data.model, appliance.model) == 0 &&
                    current->data.price == appliance.price &&
                    current->data.quantity == appliance.quantity)
                {
                    return 1;
                }
                current = current->next;
            }
    
            return 0;
        }
    
        // 按单价从小到大的顺序排序单链表
        void sort(LinkedList * list)
        {
            int i, j;
            Node *current;
            Node *next;
            Appliance temp;
    
            // 使用冒泡排序算法排序单链表
            for (i = 0; i < list->size - 1; i++)
            {
                current = list->head;
                next = current->next;
                for (j = 0; j < list->size - 1 - i; j++)
                {
                    if (current->data.price > next->data.price)
                    {
                        temp = current->data;
                        current->data = next->data;
                        next->data = temp;
                    }
                    current = current->next;
                    next = current->next;
                }
            }
            int main()
            {
                LinkedList list;
                Appliance appliance;
                int choice;
    
                // 初始化单链表
                init(&list);
    
                while (1)
                {
                    printf("\n1. 添加家电\n");
                    printf("2. 删除家电\n");
                    printf("3. 查询家电\n");
                    printf("4. 排序\n");
                    printf("5. 退出\n");
                    printf("请输入你的选择: ");
                    scanf("%d", &choice);
    
                    switch (choice)
                    {
                    case 1:
                        printf("请输入家电信息 (名称 型号 单价 数量): ");
                        scanf("%s %s %f %d", appliance.name, appliance.model, &appliance.price, &appliance.quantity);
                        insert(&list, appliance);
                        printf("添加成功!\n");
                        break;
                    case 2:
                        printf("请输入要删除的家电信息 (名称 型号 单价 数量): ");
                        scanf("%s %s %f %d", appliance.name, appliance.model, &appliance.price, &appliance.quantity);
                        delete (&list, appliance);
                        printf("删除成功!\n");
                        break;
                    case 3:
                        printf("请输入要查询的家电信息 (名称 型号 单价 数量): ");
                        scanf("%s %s %f %d", appliance.name, appliance.model, &appliance.price, &appliance.quantity);
                        if (search(&list, appliance))
                        {
                            printf("找到了!\n");
                        }
                        else
                        {
                            printf("没有找到!\n");
                        }
                        break;
                    case 4:
                        sort(&list);
                        printf("排序成功!\n");
                        break;
                    case 5:
                        exit(0);
                        break;
                    default:
                        printf("无效的选择!\n");
                        break;
                    }
                }
    
                return 0;
            }
    

    望采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 1月8日
  • 已采纳回答 12月31日
  • 创建了问题 12月30日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效