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

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

#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;
        }

望采纳。

https://blog.csdn.net/weixin_65617488/article/details/122435919

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 15
typedef struct product
{
    char name[N];
    char brand[N];
    int amount;
    double price;
    struct product *next;
}PRO;
/*生成一个新的节点,并为该节点赋初值,返回指向新节点的指针*/
PRO *Createnode()
{
    PRO *p;
    p=(PRO *)malloc(sizeof(PRO));
    if(p==NULL)
    {
        printf("NO enough memory!\n");
        exit(0);
    }
    p->next=NULL;
    strcpy(p->name," ");
    strcpy(p->brand," ");
    p->amount =0;
    p->price=1;
    return p;
}
void Insertdata(PRO *head)//用于进货,同时考虑到进同一商品的问题
{
    PRO *p = head,*s = head,*q;
    p = Createnode();
    printf("请输入进货的名称、品牌、价格和数量:\n");
    scanf("%s %s %lf %d",p->name,p->brand,&p->price,&p->amount);
    if(head->next ==NULL)
    {
        head->next = p;
    }
    else
    {
        while((p->price)<(s->next->price))
        {
            s=s->next;
            if(s->next==NULL)
            {break;}
        }
        if(s->next !=NULL)
        {
            if(strcmp(p->name,s->next->name)==0&&strcmp(p->brand,s->next->brand)==0)
        {
            s->next->amount+=p->amount;
        }
        else
            {
                q = s->next;
                s->next = p;
                p->next = q;
            }
        }
        else
        {
             s->next = p;
            p->next = NULL;
        }
    }
}
void Deletedata(PRO *head)//用于提货,同时考虑到库存不够的问题
{
    PRO *q = head,*p = head,*s,*r;
    p = Createnode();
    printf("请输入取货的名称、品牌、价格、数量:\n");
    scanf("%s %s %lf %d",p->name,p->brand,&p->price,&p->amount);
    if(head->next==NULL)
    {
        printf("暂无库存!\n");
    }
    else
        {
        while(strcmp(q->next->brand,p->brand)!=0||strcmp(q->next->name,p->name)!=0)
            {
                q = q->next;
                if(q->next ==NULL)
                    {break;}
            }
        if(q->next !=NULL)
            {if(q->next->amount == p->amount)
                {
                    if(q->next->next==NULL)
                    {
                        s = q->next;
                        q->next=NULL;
                        free(s);
                    }
                    else
                    {
                        s = q->next->next;
                        r = q->next;
                        q->next = s;
                        free(r);
                    }
                }
                else
                {
                    if(q->next->amount <p->amount)
                        printf("库存不够!请重新选择操作!\n\n");
                    else
                        q->next->amount -=p->amount;
                }
                }
            else
            {
                if(p->amount==q->amount)
                {
                    free(q);
                }
                else
                q->amount -=p->amount;
            }
        }
}
void Printdata(PRO *head)//打印库存商品信息
{
    PRO *p=head->next;
    printf("名称\t品牌\t单价\t\t数量:\n");
    while(p!=NULL)
    {
        printf("%s\t%s\t%lf\t%d\n",p->name,p->brand,p->price,p->amount);
        p = p->next;
    }
    printf("\n\n");
}
void Updatedata(PRO *head)
{
    PRO *p,*q=head,*s;
    p = Createnode();
    printf("请输入变动产品的名称、品牌、现价和数量:\n");
    scanf("%s %s %lf %d",p->name,p->brand,&p->price,&p->amount);
    while(strcmp(q->next->brand,p->brand)!=0||strcmp(q->next->name,p->name)!=0)
    {
        q=q->next;
    }
    if(q->next->next==NULL)
    {
        s = q->next;
        q->next = p;
        p->next = NULL;
        free(s);
    }
    else
    {
        s = q->next;
        p->next = s->next;
        q->next = p;
        free(s);
    }
}
int Menu()
{
    int x;
    printf("家电营业系统:\n");
    printf("1.营业开始\n");
    printf("2.进货\n");
    printf("3.提货\n");
    printf("4.更新信息\n");
    printf("5.查询信息\n");
    printf("6.退出系统\n\n");
    printf("请选择你要执行的操作:\n");
    scanf("%d",&x);
    return x;
}
void Readfile(PRO *head)//从List.txt中读入数据
{
    FILE *fp;
    PRO *p=head,*q;
    q=Createnode();
    char name[N],brand[N];
    int price=0,amount=0;
    fp=fopen("List.txt","r");
    if(fp==NULL)
    {
        printf("读取文件List.txt失败!\n");
        exit(0);
    }
    fseek(fp,22,0);
    while(fscanf(fp,"%s",q->name)!=EOF)
    {
        fscanf(fp,"%s",q->brand);
        fscanf(fp,"%lf",&q->price);
        fscanf(fp,"%d\n",&q->amount);
        p->next=q;
        p=q;
        q=Createnode();
    }
    p->next=NULL;
    fclose(fp);
}
int Savefile(PRO *head)//保存数据
{
    FILE *fp;
    PRO *p=head->next;
    fp=fopen("List.txt","w");
    if(fp==NULL)
    {
        printf("写入文件List.txt失败!\n");
        return -1;
    }
    fprintf(fp,"名称\t 品牌\t 价格\t 数量\n");
    while(p!=NULL)
    {
        fprintf(fp,"%s\t %s\t %lf\t %d\n",p->name,p->brand,p->price,p->amount);
        p=p->next;
    }
    fprintf(fp,"\n");
    fclose(fp);
    return 0;
}
void Createlist(PRO *head)
{
    Readfile(head);
    printf("营业开始,请继续选择需要执行的操作:\n");
}
int main()
{
    PRO *head;
    char ch;
    head=Createnode();
    printf("注意:首次进入系统必须选择1号!\n");
    while(1)
    {
        ch = Menu();
        switch(ch)
        {
            case 1:Createlist(head);
            break;
            case 2:Insertdata(head);
            break;
            case 3:Deletedata(head);
            break;
            case 4:Updatedata(head);
            break;
            case 5:Printdata(head);
            break;
            case 6:printf("营业结束!数据已保存至文件!");
            Savefile(head);
            exit(0);
            default:printf("请输入正确序号!\n");
        }
    }
    return 0;
}

你看看这篇实例【2019.6.24-2019.6.28(实训数据结构)1.商品管理系统实训c++(实现的基本功能:初始化,创建表,插入,删除,更新,查询,链表数据与文件之间的转换)】,链接:https://www.cnblogs.com/zhying99/p/11076248.html

借鉴下、

https://www.doc88.com/p-8458057152161.html?r=1

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

typedef struct Node {
  char name[50];
  char model[50];
  float price;
  int quantity;
  struct Node *next;
} Node;

// 初始化链表
Node *init(void) {
  Node *head = (Node *)malloc(sizeof(Node));
  head->next = NULL;
  return head;
}

// 创建链表
Node *create(void) {
  Node *head = init();
  Node *p = head;

  printf("Enter the number of appliances: ");
  int num;
  scanf("%d", &num);

  for (int i = 0; i < num; i++) {
    Node *new_node = (Node *)malloc(sizeof(Node));

    printf("Enter appliance name: ");
    scanf("%s", new_node->name);
    printf("Enter appliance model: ");
    scanf("%s", new_node->model);
    printf("Enter appliance price: ");
    scanf("%f", &new_node->price);
    printf("Enter appliance quantity: ");
    scanf("%d", &new_node->quantity);

    p->next = new_node;
    p = new_node;
  }

  p->next = NULL;
  return head;
}

// 插入一个新的家电
void insert(Node *head, Node *new_node) {
  Node *p = head;

  // 找到要插入的位置
  while (p->next && p->next->price < new_node->price) {
    p = p->next;
  }

  // 插入新的家电
  new_node->next = p->next;
  p->next = new_node;
}

// 删除一个家电
void delete(Node *head, float price) {
  Node *p = head;

  // 找到要删除的位置
  while (p->next && p->next->price != price) {
    p = p->next;
  }

  // 删除家电
  if (p->next) {
    Node *q = p->next;
    p->next = q->next;
    free(q);
  }
}

// 查询一个家电
Node *search(Node *head, float price) {
  Node *p = head->next;

  // 查找家电
  while (p && p->price != price) {
    p = p->next;
  }

  return p;
}