鱼C论坛

 找回密码
 立即注册
查看: 3629|回复: 7

[已解决]大佬帮指导一下链表,有点懵!

[复制链接]
发表于 2019-9-10 15:08:21 | 显示全部楼层 |阅读模式

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

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

x
  1. # include <stdio.h>
  2. # include <malloc.h>
  3. # include <stdlib.h>

  4. struct BOOK
  5. {
  6.         char title[30];                //数据域 书名
  7.         char author[30];        //数据域 作者

  8.         struct BOOK *next;        //指针域 【下一个】
  9. };

  10. void getinput(struct BOOK* book)                //获取输入
  11. {
  12.         printf("请输入书名: ");
  13.         scanf("%s", book->title);
  14.         printf("请输入作者: ");
  15.         scanf("%s", book->author);
  16. }

  17. void addBook(struct BOOK **library)                //
  18. {//这里传参main里面是&library,不能直接library传参  这边接收参数直接*library么;library不是一个地址么?为何还要取地址这边接收**?
  19.         struct BOOK* book , *temp;

  20.         book = (struct BOOK*)malloc(sizeof(struct BOOK));
  21.         if (book == NULL)                //
  22.         {
  23.                 printf("内存分配失败\n");
  24.                 exit(1);
  25.         }
  26.         getinput(book);                //

  27.         if (*library != 0)                //
  28.         {       
  29.                 temp = *library;         
  30.                 *library = book;
  31.                 book->next = temp;
  32.         }
  33.         else                        //
  34.         {
  35.                 *library = book;
  36.                 book->next = NULL;
  37.         }
  38. }

  39. void print(struct BOOK* library)
  40. {
  41.         struct BOOK* book;
  42.         int count = 1;                //计数

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

  53. void freelibrary(struct BOOK* library)
  54. {
  55.         while (library != NULL)
  56.         {
  57.                 free(library);
  58.                 library = library->next;
  59.         }
  60. }

  61. int main(void)
  62. {
  63.         struct BOOK* library = NULL;        //
  64.         char ch;

  65.         while (1)
  66.         {
  67.                 printf("请问是否需要录入书籍信息(y / n): ");
  68.                 do
  69.                 {
  70.                         ch = getchar();
  71.                 } while (ch != 'y' && ch != 'n');
  72.                
  73.                 if (ch == 'y')
  74.                 {
  75.                         addBook(&library);
  76.                 }
  77.                 else
  78.                 {
  79.                         break;
  80.                 }
  81.         }

  82.         printf("是否输出图书信息(y / n): ");
  83.         do
  84.         {
  85.                 ch = getchar();
  86.         } while (ch != 'y' && ch != 'n');

  87.         if (ch == 'y')
  88.         {
  89.                 print(library);
  90.         }

  91.         freelibrary(library);                //释放内存

  92.         return 0;
  93. }
复制代码


求大佬帮注释一下啊,直接看了几遍就是半懂不懂的,有个比较详细的注释就好了,代码也是看教程边理解边打,打完了,又看了几次代码好像懂了点,再看一下又好像不懂了,晕死,我打//的就是求帮注释的,不过大佬看到还有什么关键点的也可以帮我加上去,十分感谢
为何用vs2019执行过程中会出错,其他编译器都不会,全部正常,无语!
最佳答案
2019-9-10 19:05:21
本帖最后由 superbe 于 2019-9-12 08:48 编辑

freeLibrary函数有点问题,改过了,也加了注释。

  1. # include <stdio.h>
  2. # include <malloc.h>
  3. # include <stdlib.h>

  4. struct BOOK
  5. {
  6.         char title[30];         //数据域 书名
  7.         char author[30];        //数据域 作者

  8.         struct BOOK *next;      //指针域 (指向下一BOOK)
  9. };

  10. void getinput(struct BOOK* book)        //获取输入
  11. {
  12.         printf("请输入书名: ");
  13.         scanf("%s", book->title);
  14.         printf("请输入作者: ");
  15.         scanf("%s", book->author);
  16. }

  17. void addBook(struct BOOK **library)        //library是一个二级指针
  18. {                                          //*library是library指向的BOOK地址
  19.                                             //**library是这个BOOK地址保存的BOOK结构
  20.                                             //注意实参和形参名称相同,意义不同
  21.         struct BOOK* book , *temp;
  22.         book = (struct BOOK*)malloc(sizeof(struct BOOK));
  23.         if (book == NULL)                  //malloc函数返回NULL表示内存分配失败
  24.         {
  25.                 printf("内存分配失败\n");
  26.                 exit(1);
  27.         }
  28.         getinput(book);                    //为刚添加的BOOK结构输入信息(书名,作者)

  29.         if (*library != 0)                 //如果第一个BOOK地址有效(不为0),说明已经添加过第一本BOOK了
  30.         {                                  //那么在此BOOK前面继续添加一个BOOK,成为新的链表头
  31.                 temp = *library;
  32.                 *library = book;
  33.                 book->next = temp;
  34.         }
  35.         else                               //否则,说明链表为空,那么新建一个BOOK
  36.         {
  37.                 *library = book;
  38.                 book->next = NULL;
  39.         }
  40. }

  41. void print(struct BOOK* library)        //打印链表每个BOOK的信息(与添加的顺序相反)
  42. {
  43.         struct BOOK* book;
  44.         int count = 1;                //计数

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

  55. void freelibrary(struct BOOK* library)        //这个函数有问题,已经修改过了
  56. {
  57.         struct BOOK* temp;
  58.                        
  59.         while (library != NULL)
  60.         {
  61.                 temp=library->next;
  62.                 free(library);
  63.                 //library = library->next;        //问题就在这,上面library已经被free了怎么还能访问library->next呢
  64.                 library=temp;
  65.         }
  66. }

  67. int main(void)
  68. {
  69.         struct BOOK* library = NULL;    //链表头指针,它里面保存的是第一个Book的地址
  70.         char ch;

  71.         while (1)
  72.         {
  73.                 printf("请问是否需要录入书籍信息(y / n): ");
  74.                 do
  75.                 {
  76.                         ch = getchar();
  77.                 } while (ch != 'y' && ch != 'n');
  78.                   
  79.                 if (ch == 'y')
  80.                 {
  81.                         addBook(&library);    //传递的参数是library变量本身的地址
  82.                 }
  83.                 else
  84.                 {
  85.                         break;
  86.                 }
  87.         }

  88.         printf("是否输出图书信息(y / n): ");
  89.         do
  90.         {
  91.                 ch = getchar();
  92.         } while (ch != 'y' && ch != 'n');

  93.         if (ch == 'y')
  94.         {
  95.                 print(library);        //打印library指向的链表信息
  96.         }

  97.         freelibrary(library);         //释放library指向的链表内存

  98.         return 0;
  99. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-10 19:05:21 | 显示全部楼层    本楼为最佳答案   
本帖最后由 superbe 于 2019-9-12 08:48 编辑

freeLibrary函数有点问题,改过了,也加了注释。

  1. # include <stdio.h>
  2. # include <malloc.h>
  3. # include <stdlib.h>

  4. struct BOOK
  5. {
  6.         char title[30];         //数据域 书名
  7.         char author[30];        //数据域 作者

  8.         struct BOOK *next;      //指针域 (指向下一BOOK)
  9. };

  10. void getinput(struct BOOK* book)        //获取输入
  11. {
  12.         printf("请输入书名: ");
  13.         scanf("%s", book->title);
  14.         printf("请输入作者: ");
  15.         scanf("%s", book->author);
  16. }

  17. void addBook(struct BOOK **library)        //library是一个二级指针
  18. {                                          //*library是library指向的BOOK地址
  19.                                             //**library是这个BOOK地址保存的BOOK结构
  20.                                             //注意实参和形参名称相同,意义不同
  21.         struct BOOK* book , *temp;
  22.         book = (struct BOOK*)malloc(sizeof(struct BOOK));
  23.         if (book == NULL)                  //malloc函数返回NULL表示内存分配失败
  24.         {
  25.                 printf("内存分配失败\n");
  26.                 exit(1);
  27.         }
  28.         getinput(book);                    //为刚添加的BOOK结构输入信息(书名,作者)

  29.         if (*library != 0)                 //如果第一个BOOK地址有效(不为0),说明已经添加过第一本BOOK了
  30.         {                                  //那么在此BOOK前面继续添加一个BOOK,成为新的链表头
  31.                 temp = *library;
  32.                 *library = book;
  33.                 book->next = temp;
  34.         }
  35.         else                               //否则,说明链表为空,那么新建一个BOOK
  36.         {
  37.                 *library = book;
  38.                 book->next = NULL;
  39.         }
  40. }

  41. void print(struct BOOK* library)        //打印链表每个BOOK的信息(与添加的顺序相反)
  42. {
  43.         struct BOOK* book;
  44.         int count = 1;                //计数

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

  55. void freelibrary(struct BOOK* library)        //这个函数有问题,已经修改过了
  56. {
  57.         struct BOOK* temp;
  58.                        
  59.         while (library != NULL)
  60.         {
  61.                 temp=library->next;
  62.                 free(library);
  63.                 //library = library->next;        //问题就在这,上面library已经被free了怎么还能访问library->next呢
  64.                 library=temp;
  65.         }
  66. }

  67. int main(void)
  68. {
  69.         struct BOOK* library = NULL;    //链表头指针,它里面保存的是第一个Book的地址
  70.         char ch;

  71.         while (1)
  72.         {
  73.                 printf("请问是否需要录入书籍信息(y / n): ");
  74.                 do
  75.                 {
  76.                         ch = getchar();
  77.                 } while (ch != 'y' && ch != 'n');
  78.                   
  79.                 if (ch == 'y')
  80.                 {
  81.                         addBook(&library);    //传递的参数是library变量本身的地址
  82.                 }
  83.                 else
  84.                 {
  85.                         break;
  86.                 }
  87.         }

  88.         printf("是否输出图书信息(y / n): ");
  89.         do
  90.         {
  91.                 ch = getchar();
  92.         } while (ch != 'y' && ch != 'n');

  93.         if (ch == 'y')
  94.         {
  95.                 print(library);        //打印library指向的链表信息
  96.         }

  97.         freelibrary(library);         //释放library指向的链表内存

  98.         return 0;
  99. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-11 04:12:49 | 显示全部楼层
superbe 发表于 2019-9-10 19:05
freeLibrary函数有点问题,改过了,也加了注释。

十分感谢,有了这个注释,起码理解了一大半
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-12 09:05:04 | 显示全部楼层
*library是library指向的BOOK地址
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-4 23:57:49 | 显示全部楼层
跟帖。笔记
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-5 00:04:55 | 显示全部楼层
本帖最后由 召唤师 于 2020-3-5 00:11 编辑

1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-5 00:08:35 | 显示全部楼层
本帖最后由 召唤师 于 2020-3-5 00:11 编辑

1不好意思。错了。编辑了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-3-16 14:43:12 | 显示全部楼层
看这节课晕了很久,分析下来最绕人的就是这个library.
main方法里的library是指向Book的指针
addBook方法中的library是指向 指向Book指针的指针.
所以导致很多人都在这里晕了...
大佬的注释很清晰了,关键就是这一句" //注意实参和形参名称相同,意义不同"
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 17:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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