【数据布局】双向循环链表实现浅易图书管理系统的增删改查 ...

打印 上一主题 下一主题

主题 1020|帖子 1020|积分 3060

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. // 书籍结构体
  5. struct book {
  6.     char name[20];      // 书名
  7.     char authorname[20]; // 作者名
  8.     float price;        // 价格
  9.     int num;            // 总数量
  10.     int borrowed;       // 已借出数量
  11. };
  12. // 双向循环链表节点
  13. struct node {
  14.     struct book data;
  15.     struct node *prev;
  16.     struct node *next;
  17. };
  18. // 全局变量:链表头指针
  19. struct node *head = NULL;
  20. // 函数声明
  21. void add_book();
  22. void delete_book();
  23. void modify_book();
  24. void search_book();
  25. void borrow_book();
  26. void return_book();
  27. void display_all();
  28. void free_list();
  29. struct node* find_book(char *name);
  30. int main() {
  31.     int choice;
  32.     while (1) {
  33.         printf("\n图书管理系统\n");
  34.         printf("1. 添加书籍\n");
  35.         printf("2. 删除书籍\n");
  36.         printf("3. 修改书籍信息\n");
  37.         printf("4. 查询书籍信息\n");
  38.         printf("5. 借书\n");
  39.         printf("6. 还书\n");
  40.         printf("7. 显示所有书籍\n");
  41.         printf("0. 退出\n");
  42.         printf("请选择操作: ");
  43.         scanf("%d", &choice);
  44.         switch (choice) {
  45.             case 1: add_book(); break;
  46.             case 2: delete_book(); break;
  47.             case 3: modify_book(); break;
  48.             case 4: search_book(); break;
  49.             case 5: borrow_book(); break;
  50.             case 6: return_book(); break;
  51.             case 7: display_all(); break;
  52.             case 0: free_list(); return 0;
  53.             default: printf("无效选择!\n");
  54.         }
  55.     }
  56.     return 0;
  57. }
  58. // 添加书籍
  59. void add_book() {
  60.     struct node *new_node = (struct node*)malloc(sizeof(struct node));
  61.     if (!new_node) {
  62.         printf("内存分配失败!\n");
  63.         return;
  64.     }
  65.     printf("请输入书名: ");
  66.     scanf("%s", new_node->data.name);
  67.     printf("请输入作者名: ");
  68.     scanf("%s", new_node->data.authorname);
  69.     printf("请输入价格: ");
  70.     scanf("%f", &new_node->data.price);
  71.     printf("请输入总数量: ");
  72.     scanf("%d", &new_node->data.num);
  73.     new_node->data.borrowed = 0;
  74.     if (!head) {
  75.         head = new_node;
  76.         head->next = head;
  77.         head->prev = head;
  78.     } else {
  79.         new_node->next = head;
  80.         new_node->prev = head->prev;
  81.         head->prev->next = new_node;
  82.         head->prev = new_node;
  83.     }
  84.     printf("书籍添加成功!\n");
  85. }
  86. // 删除书籍
  87. void delete_book() {
  88.     if (!head) {
  89.         printf("没有书籍可删除!\n");
  90.         return;
  91.     }
  92.     char name[20];
  93.     printf("请输入要删除的书名: ");
  94.     scanf("%s", name);
  95.     struct node *current = head;
  96.     do {
  97.         if (strcmp(current->data.name, name) == 0) {
  98.             if (current->data.borrowed > 0) {
  99.                 printf("该书还有借出未还,不能删除!\n");
  100.                 return;
  101.             }
  102.             if (current == head) {
  103.                 if (head->next == head) {
  104.                     head = NULL;
  105.                 } else {
  106.                     head = head->next;
  107.                 }
  108.             }
  109.             current->prev->next = current->next;
  110.             current->next->prev = current->prev;
  111.             free(current);
  112.             printf("书籍删除成功!\n");
  113.             return;
  114.         }
  115.         current = current->next;
  116.     } while (current != head);
  117.     printf("未找到该书!\n");
  118. }
  119. // 修改书籍信息
  120. void modify_book() {
  121.     if (!head) {
  122.         printf("没有书籍可修改!\n");
  123.         return;
  124.     }
  125.     char name[20];
  126.     printf("请输入要修改的书名: ");
  127.     scanf("%s", name);
  128.     struct node *book = find_book(name);
  129.     if (!book) {
  130.         printf("未找到该书!\n");
  131.         return;
  132.     }
  133.     printf("当前信息:\n");
  134.     printf("书名: %s\n", book->data.name);
  135.     printf("作者: %s\n", book->data.authorname);
  136.     printf("价格: %.2f\n", book->data.price);
  137.     printf("总数量: %d\n", book->data.num);
  138.     printf("已借出: %d\n", book->data.borrowed);
  139.     printf("\n请输入新的作者名: ");
  140.     scanf("%s", book->data.authorname);
  141.     printf("请输入新的价格: ");
  142.     scanf("%f", &book->data.price);
  143.     printf("请输入新的总数量: ");
  144.     scanf("%d", &book->data.num);
  145.     printf("书籍信息修改成功!\n");
  146. }
  147. // 查询书籍信息
  148. void search_book() {
  149.     if (!head) {
  150.         printf("没有书籍可查询!\n");
  151.         return;
  152.     }
  153.     char name[20];
  154.     printf("请输入要查询的书名: ");
  155.     scanf("%s", name);
  156.     struct node *book = find_book(name);
  157.     if (!book) {
  158.         printf("未找到该书!\n");
  159.         return;
  160.     }
  161.     printf("\n书籍信息:\n");
  162.     printf("书名: %s\n", book->data.name);
  163.     printf("作者: %s\n", book->data.authorname);
  164.     printf("价格: %.2f\n", book->data.price);
  165.     printf("总数量: %d\n", book->data.num);
  166.     printf("可借数量: %d\n", book->data.num - book->data.borrowed);
  167. }
  168. // 借书
  169. void borrow_book() {
  170.     if (!head) {
  171.         printf("没有书籍可借!\n");
  172.         return;
  173.     }
  174.     char name[20];
  175.     printf("请输入要借的书名: ");
  176.     scanf("%s", name);
  177.     struct node *book = find_book(name);
  178.     if (!book) {
  179.         printf("未找到该书!\n");
  180.         return;
  181.     }
  182.     int available = book->data.num - book->data.borrowed;
  183.     printf("当前可借数量: %d\n", available);
  184.    
  185.     if (available <= 0) {
  186.         printf("该书已全部借出!\n");
  187.         return;
  188.     }
  189.     int quantity;
  190.     printf("请输入要借的数量: ");
  191.     scanf("%d", &quantity);
  192.    
  193.     if (quantity <= 0) {
  194.         printf("借书数量必须大于0!\n");
  195.         return;
  196.     }
  197.    
  198.     if (quantity > available) {
  199.         printf("借书数量超过可借数量!\n");
  200.         return;
  201.     }
  202.     book->data.borrowed += quantity;
  203.     printf("借书成功! 当前已借出: %d\n", book->data.borrowed);
  204. }
  205. // 还书
  206. void return_book() {
  207.     if (!head) {
  208.         printf("没有书籍可还!\n");
  209.         return;
  210.     }
  211.     char name[20];
  212.     printf("请输入要还的书名: ");
  213.     scanf("%s", name);
  214.     struct node *book = find_book(name);
  215.     if (!book) {
  216.         printf("未找到该书!\n");
  217.         return;
  218.     }
  219.     if (book->data.borrowed <= 0) {
  220.         printf("该书没有借出记录!\n");
  221.         return;
  222.     }
  223.     printf("当前已借出数量: %d\n", book->data.borrowed);
  224.    
  225.     int quantity;
  226.     printf("请输入要还的数量: ");
  227.     scanf("%d", &quantity);
  228.    
  229.     if (quantity <= 0) {
  230.         printf("还书数量必须大于0!\n");
  231.         return;
  232.     }
  233.    
  234.     if (quantity > book->data.borrowed) {
  235.         printf("还书数量超过已借出数量!\n");
  236.         return;
  237.     }
  238.     book->data.borrowed -= quantity;
  239.     printf("还书成功! 当前已借出: %d\n", book->data.borrowed);
  240. }
  241. // 显示所有书籍
  242. void display_all() {
  243.     if (!head) {
  244.         printf("没有书籍可显示!\n");
  245.         return;
  246.     }
  247.     struct node *current = head;
  248.     printf("\n所有书籍信息:\n");
  249.     do {
  250.         printf("\n书名: %s\n", current->data.name);
  251.         printf("作者: %s\n", current->data.authorname);
  252.         printf("价格: %.2f\n", current->data.price);
  253.         printf("总数量: %d\n", current->data.num);
  254.         printf("可借数量: %d\n", current->data.num - current->data.borrowed);
  255.         current = current->next;
  256.     } while (current != head);
  257. }
  258. // 释放链表内存
  259. void free_list() {
  260.     if (!head) return;
  261.     struct node *current = head;
  262.     struct node *temp;
  263.     do {
  264.         temp = current;
  265.         current = current->next;
  266.         free(temp);
  267.     } while (current != head);
  268. }
  269. // 查找书籍
  270. struct node* find_book(char *name) {
  271.     if (!head) return NULL;
  272.     struct node *current = head;
  273.     do {
  274.         if (strcmp(current->data.name, name) == 0) {
  275.             return current;
  276.         }
  277.         current = current->next;
  278.     } while (current != head);
  279.     return NULL;
  280. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

知者何南

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表