鱼C论坛

 找回密码
 立即注册
查看: 1734|回复: 5

[已解决]代码中的library是真么意思,和图片中的变量a有什么关系

[复制链接]
发表于 2018-10-13 16:34:54 | 显示全部楼层 |阅读模式

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

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

x
图片中的内容全部理解,但是代码中的library是什么意思,和图片中的变量a有什么关系
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Book
  4. {
  5.         char title[128];
  6.         char author[40];
  7.         struct Book *next;
  8. };

  9. void getInput(struct Book *book);
  10. void addBook(struct Book **library);
  11. void printLibrary(struct Book *library);
  12. void releaseLibrary(struct Book *library);

  13. void getInput(struct Book *book)
  14. {
  15.         printf("请输入书名:");
  16.         scanf("%s", book->title);
  17.         printf("请输入作者:");
  18.         scanf("%s", book->author);
  19. }

  20. void addBook(struct Book **library)
  21. {
  22.         struct Book *book, *temp;

  23.         book = (struct Book *)malloc(sizeof(struct Book));
  24.         if (book == NULL)
  25.         {
  26.                 printf("内存分配失败了!\n");
  27.                 exit(1);
  28.         }

  29.         getInput(book);

  30.         if (*library != NULL)
  31.         {
  32.                 temp = *library;
  33.                 *library = book;
  34.                 book->next = temp;       
  35.         }
  36.         else
  37.         {
  38.                 *library = book;
  39.                 book->next = NULL;
  40.         }
  41. }

  42. void printLibrary(struct Book *library)
  43. {
  44.         struct Book *book;
  45.         int count = 1;

  46.         book = library;
  47.         while (book != NULL)
  48.         {
  49.                 printf("Book%d: ", count);
  50.                 printf("书名: %s\n", book->title);
  51.                 printf("作者: %s\n", book->author);
  52.                 book = book->next;
  53.                 count++;
  54.         }
  55. }

  56. void releaseLibrary(struct Book *library)
  57. {
  58.         while (library != NULL)
  59.         {
  60.                 free(library);
  61.                 library = library->next;
  62.         }
  63. }

  64. int main(void)
  65. {
  66.         struct Book *library = NULL;
  67.         int ch;

  68.         while (1)
  69.         {
  70.                 printf("请问是否需要录入书籍信息(Y/N):");
  71.                 do
  72.                 {
  73.                         ch = getchar();
  74.                 } while (ch != 'Y' && ch != 'N');

  75.                 if (ch == 'Y')
  76.                 {
  77.                         addBook(&library);
  78.                 }
  79.                 else
  80.                 {
  81.                         break;
  82.                 }
  83.         }

  84.         printf("请问是否需要打印图书信息(Y/N):");
  85.         do
  86.         {
  87.                 ch = getchar();
  88.         } while (ch != 'Y' && ch != 'N');

  89.         if (ch == 'Y')
  90.         {
  91.                 printLibrary(library);
  92.         }

  93.         releaseLibrary(library);

  94.         return 0;
  95. }
复制代码



最佳答案
2018-10-13 16:50:00
library 就是图书馆~
你每一次透过 addBook 增加一本书
temp = *library;
*library = book;
book->next = temp;
有一些差别在于你是头插法或尾插法跟插入指定位置而已,手法一样
1.PNG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-13 16:50:00 | 显示全部楼层    本楼为最佳答案   
library 就是图书馆~
你每一次透过 addBook 增加一本书
temp = *library;
*library = book;
book->next = temp;
有一些差别在于你是头插法或尾插法跟插入指定位置而已,手法一样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-13 17:02:50 | 显示全部楼层
claws0n 发表于 2018-10-13 16:50
library 就是图书馆~
你每一次透过 addBook 增加一本书
temp = *library;

1.图片中是 插入指定位置吗
2.library 怎么觉得像 结构体数组,往数组里放元素。library 是图中的 空链表吗
捕获.PNG
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-13 17:11:14 | 显示全部楼层
大可爱 发表于 2018-10-13 17:02
1.图片中是 插入指定位置吗
2.library 怎么觉得像 结构体数组,往数组里放元素。library 是图中的 空链 ...

1.那是空链表,跟插入法没有关系
2.  #77.  struct Book *library = NULL;   // 是结构体指针
透过 #90 addBook,addBook 里是 malloc
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-13 17:57:35 | 显示全部楼层
claws0n 发表于 2018-10-13 17:11
1.那是空链表,跟插入法没有关系
2.  #77.  struct Book *library = NULL;   // 是结构体指针
透过 #90 ...

这个是指定位置插入吗

这是指定位置插入吗

这是指定位置插入吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-13 18:15:14 | 显示全部楼层
大可爱 发表于 2018-10-13 17:57
这个是指定位置插入吗

没有说,但是一般情况,不是头尾都是指定位置
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-12 19:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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