鱼C论坛

 找回密码
 立即注册
查看: 1084|回复: 1

[作品展示] S1E46:单链表2--笔记

[复制链接]
发表于 2023-11-30 12:13:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. //鱼C论坛——BaysideLizard写于2023年11月30日

  5. struct Book
  6. {
  7.     char title[128];
  8.     char author[40];
  9.     struct Book *next;
  10. //申请一个单链表节点
  11. };

  12. void addBook(struct Book **headLibrary);
  13. void inputBook(struct Book *book);
  14. void printLibrary(struct Book *headLibrary);
  15. void releaseLibrary(struct Book *headLibrary);
  16. struct Book *searchBook(struct Book *library,char *searchtarget);
  17. void printsearchresult(struct Book *searchresult);


  18. int main()
  19. {
  20.     struct Book *headLibrary = NULL,*searchresult;
  21.     char ch,searchtarget[128];
  22.     while(1)
  23.     {
  24.         printf("请问是否需要录入书籍信息[Y/n]:");
  25.         do
  26.         {
  27.             ch = getchar();
  28.             if(ch != '\n')
  29.             {
  30.                 getchar();
  31.             }
  32.         }while(ch!='Y'&&ch!='N'&&ch!='y'&&ch!='n'&&ch!='\n');
  33.         if(ch == 'y' || ch == 'Y' || ch == '\n')
  34.         {
  35.             addBook(&headLibrary);
  36.         }
  37.         else
  38.         {
  39.             break;
  40.         }
  41.     }
  42.     while(1)
  43.     {
  44.         printf("请问是否需要输出书籍信息[Y/n]:");
  45.         do
  46.         {
  47.             ch = getchar();
  48.             if(ch != '\n')
  49.             {
  50.                 getchar();
  51.             }
  52.         }while(ch!='Y'&&ch!='N'&&ch!='y'&&ch!='n'&&ch!='\n');
  53.         if(ch == 'y' || ch == 'Y' || ch == '\n')
  54.         {
  55.             printLibrary(headLibrary);
  56.             break;
  57.         }
  58.         else
  59.         {
  60.             break;
  61.         }
  62.     }

  63. searchbook:
  64.     printf("\n请输入要搜索的书名或作者:");
  65.     scanf("%s",searchtarget);
  66.     getchar();
  67.     searchresult = searchBook(headLibrary,searchtarget);
  68.     if(searchresult == NULL)
  69.     {
  70.         printf("很抱歉,没能找到:(\n");
  71.         printf("请问是否需要重新查找[y/N]:");
  72.         while(1)
  73.         {
  74.             do
  75.             {
  76.                 ch = getchar();
  77.                 if(ch != '\n')
  78.                 {
  79.                     getchar();
  80.                 }
  81.             }while(ch!='Y'&&ch!='N'&&ch!='y'&&ch!='n'&&ch!='\n');
  82.             if(ch == 'n' || ch == 'Y' || ch == '\n')
  83.             {
  84.                 break;
  85.             }
  86.             else
  87.             {
  88.                 goto searchbook;
  89.             }
  90.         }
  91.     }
  92.     else
  93.     {
  94.         do
  95.         {
  96.             printf("已找到相关图书:\n");
  97.             printsearchresult(searchresult);
  98.         }while(searchBook(searchresult->next,searchtarget) != NULL);
  99.     }

  100.     releaseLibrary(headLibrary);

  101.     return 0;
  102. }

  103. //插入一个单链表节点(尾插法)
  104. void addBook(struct Book **headLibrary)
  105. {
  106.     struct Book *book,*temp;
  107.     static struct Book *tail;
  108.     book = (struct Book *)malloc(sizeof(struct Book));
  109.     if(book == NULL)
  110.     {
  111.         printf("Memory allocation failed :(");
  112.         exit(1);
  113.     }
  114.     inputBook(book);

  115.     if(*headLibrary != NULL)
  116.     {
  117. //插入数据
  118.         tail->next = book;
  119.         book->next = NULL;

  120.     }
  121.     else
  122.     {
  123.         *headLibrary = book;
  124.         book->next = NULL;
  125.     }
  126. //修改指向单链表尾部的指针使之指向新的尾部
  127.     tail = book;
  128. }

  129. void inputBook(struct Book *book)
  130. {
  131.     printf("请输入书名:");
  132.     scanf("%s",book->title);
  133.     getchar();
  134.     printf("请输入作者:");
  135.     scanf("%s",book->author);
  136.     getchar();
  137. }

  138. void printLibrary(struct Book *headLibrary)
  139. {
  140.     struct Book *book;
  141.     int count = 1;

  142.     book = headLibrary;
  143.     while(book != NULL)
  144.     {
  145.         printf("Book%d:\n",count);
  146.         printf("书名:%s\n",book->title);
  147.         printf("作者:%s\n",book->author);
  148.         book = book->next;
  149.         ++count;
  150.     }
  151. }

  152. //依次释放单链表节点
  153. void releaseLibrary(struct Book *headLibrary)
  154. {
  155.     struct Book *temp;
  156.     while(headLibrary != NULL)
  157.     {
  158.         temp = headLibrary;
  159.         headLibrary = headLibrary->next;
  160.         free(temp);
  161.     }
  162. }

  163. //搜索单链表
  164. struct Book *searchBook(struct Book *library,char *searchtarget)
  165. {
  166.     struct Book *searchresult;

  167.     searchresult = library;
  168.     while(searchresult != NULL)
  169.     {
  170.         if(strcmp(searchresult->title,searchtarget) == 0 || strcmp(searchresult->author,searchtarget) == 0)
  171.         {
  172.             break;
  173.         }
  174.         searchresult = searchresult->next;
  175.     }

  176.     return searchresult;
  177. }

  178. //打印搜索结果
  179. void printsearchresult(struct Book *searchresult)
  180. {
  181.         printf("书名:%s\n",searchresult->title);
  182.         printf("作者:%s\n",searchresult->author);
  183. }
复制代码








运行结果:
请问是否需要录入书籍信息[Y/n]:y
请输入书名:《带你学C带你飞》
请输入作者:小甲鱼
请问是否需要录入书籍信息[Y/n]:
请输入书名:《零基础入门学习Scratch》
请输入作者:不二如是
请问是否需要录入书籍信息[Y/n]:Y
请输入书名:《零基础入门学习Python》
请输入作者:小甲鱼
请问是否需要录入书籍信息[Y/n]:n
请问是否需要输出书籍信息[Y/n]:
Book1:
书名:《带你学C带你飞》
作者:小甲鱼
Book2:
书名:《零基础入门学习Scratch》
作者:不二如是
Book3:
书名:《零基础入门学习Python》
作者:小甲鱼

请输入要搜索的书名或作者:大甲鱼
很抱歉,没能找到:(
请问是否需要重新查找[y/N]:y

请输入要搜索的书名或作者:小鱼
很抱歉,没能找到:(
请问是否需要重新查找[y/N]:Y

请输入要搜索的书名或作者:小甲鱼
已找到相关图书:
书名:《带你学C带你飞》
作者:小甲鱼
已找到相关图书:
书名:《零基础入门学习Python》
作者:小甲鱼

Process returned 0 (0x0)   execution time : 74.824 s
Press any key to continue.








在FishC学C的第25天
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-30 12:14:01 | 显示全部楼层
这段代码是一个简单的图书管理系统,使用单链表实现。下面是对代码的解析和修改建议。

1. 在定义Book结构体时,title和author应该为字符串类型(char数组),而不是char类型。需要修改如下:
  1. struct Book
  2. {
  3.     char title[100];
  4.     char author[100];
  5.     struct Book *next;
  6. };
复制代码

2. inputBook函数中的scanf需要使用%s格式化输入字符串,因此要将book->title和book->author改为数组名,即book->title和book->author。修改如下:
  1. void inputBook(struct Book *book)
  2. {
  3.     printf("请输入书名:");
  4.     scanf("%s", book->title);
  5.     getchar();
  6.     printf("请输入作者:");
  7.     scanf("%s", book->author);
  8.     getchar();
  9. }
复制代码

3. 在searchBook函数中,searchresult初始值应该设置为NULL。修改如下:
  1. struct Book *searchBook(struct Book *library, char *searchtarget)
  2. {
  3.     struct Book *searchresult = NULL;
  4.     // ...
  5. }
复制代码

4. 在printsearchresult函数中,输出搜索结果前需要检查searchresult是否为空。修改如下:
  1. void printsearchresult(struct Book *searchresult)
  2. {
  3.     if (searchresult != NULL)
  4.     {
  5.         printf("书名:%s\n", searchresult->title);
  6.         printf("作者:%s\n", searchresult->author);
  7.     }
  8.     else
  9.     {
  10.         printf("很抱歉,没能找到:(\n");
  11.     }
  12. }
复制代码

5. 为了避免内存泄漏,releaseLibrary函数在释放每个节点之前,需要先将headLibrary指针指向下一个节点。修改如下:
  1. void releaseLibrary(struct Book *headLibrary)
  2. {
  3.     struct Book *temp;
  4.     while (headLibrary != NULL)
  5.     {
  6.         temp = headLibrary->next;
  7.         free(headLibrary);
  8.         headLibrary = temp;
  9.     }
  10. }
复制代码

以上是对代码的修改建议,希望能帮助到你。如果还有疑问,请随时追问。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 23:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表