鱼C论坛

 找回密码
 立即注册
查看: 890|回复: 3

[已解决]照着视频打的代码,单链表部分,咋还报错了,,,

[复制链接]
发表于 2020-12-27 13:48:58 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. //*********************************单链表 ******************************
  6. //信息阈里面存放的是下一个阈的指针

  7. //单链表节点声明
  8. struct Book
  9. {
  10.         char title[128];
  11.         char author[40];
  12.         struct Book *next;
  13. };

  14. void getInput(struct Book *book)
  15. {
  16.         printf("enter the title:");
  17.         scanf("%s",book->title);
  18.         printf("enter the author:");
  19.         scanf("%s",book->author);
  20. }

  21. void addBook(struct Book **libaray)  //传入的是指向指针的结构体指针变量
  22. {
  23.         struct Book *book ,*temp;
  24.         book = (struct Book *)malloc(sizeof(struct Book)) ; // 动态申请一个结构体指针,申请一个新的节点
  25.         if (book == NULL)
  26.         {
  27.                 printf("内存分配失败");
  28.                 exit(1);
  29.         }
  30.         getInput(book);  //为新的节点填充信息阈的内容
  31.        
  32.         if(*libaray != NULL)  //头插入法
  33.         {
  34.                 temp =  *libaray;
  35.                 *libaray = book;
  36.                 book -> next = temp;
  37.         }
  38.         else  //空的单链表
  39.         {
  40.                 *library = book;   //library为空,直接指向新的节点
  41.                 book -> next = NULL;  //将book节点指向null,也就是指向单链表的结尾
  42.         }
  43. }

  44. //这些library和book没有什么实际的指向性,并不表示将谁传递给了谁
  45. //就是和正常的函数一样,就是形参和实参的关系,知识一个变量,名,实际上传递的是什么,还是有实参决定
  46. void printLibrary(struct Book *libaray)  
  47. {
  48.         struct Book *book; //只是定义了一个结构体指针,来存放 libaray这个结构体指针,没有别的什么含义
  49.         int count = 1;       
  50.         book =  libaray;  
  51.         while(book != NULL)
  52.         {
  53.                 printf("book = %d:",count);
  54.                 printf("title:%s",book->title);
  55.                 printf("author:%s",book->author);
  56.                 book = book->next;   //指向下一个节点
  57.                 count++;
  58.         }
  59. }

  60. void releaseLibrary(struct Book *library)
  61. {

  62.         while(library != NULL)
  63.         {               
  64.                 library = library->next;
  65.                 free(library);       
  66.         }
  67. }

  68. int main()
  69. {
  70.         char ch;
  71.     struct Book *libaray = NULL;  //初始化一个指针,头指针,相当于定义了一个空的单链表
  72.     addBook(&libaray);
  73.     while(1)
  74.     {
  75.             printf("do you still want to enter the info of book?");
  76.             do
  77.             {
  78.                     ch  = getchar();
  79.                 }while(ch != 'Y' && ch != 'N');
  80.                
  81.                 if(ch == 'Y')
  82.                 {
  83.                         addBook(&libaray);
  84.                 }
  85.                 else
  86.                 {
  87.                         break;
  88.                 }
  89.         }
  90.        
  91.         printf("do you need to print the info of book?");
  92.         do
  93.         {
  94.                 ch  = getchar();
  95.         }while(ch != 'Y' && ch != 'N');
  96.        
  97.         if(ch == 'Y')
  98.         {
  99.                 printLibrary(libaray) ;
  100.         }
  101.        
  102.         releaseLibrary(libaray);
  103.        
  104.         return 0;
  105. }
复制代码
最佳答案
2020-12-27 14:53:35
本帖最后由 jackz007 于 2020-12-27 14:56 编辑

        [b]变量名写错了
void addBook(struct Book **libaray)  //传入的是指向指针的结构体指针变量
{
. . . . . .
        if(*libaray != NULL)  //头插入法
. . . . . .
                temp =  *libaray;
                *libaray = book;
. . . . . .
                *library = book;   //library为空,直接指向新的节点

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

使用道具 举报

发表于 2020-12-27 13:54:14 From FishC Mobile | 显示全部楼层
释放链表的函数是错的,我没记错的话,你这期视频的下一期开头有讲
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-27 14:03:41 | 显示全部楼层
倒戈卸甲 发表于 2020-12-27 13:54
释放链表的函数是错的,我没记错的话,你这期视频的下一期开头有讲

是43行报错
释放那我改过了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-27 14:53:35 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2020-12-27 14:56 编辑

        [b]变量名写错了
void addBook(struct Book **libaray)  //传入的是指向指针的结构体指针变量
{
. . . . . .
        if(*libaray != NULL)  //头插入法
. . . . . .
                temp =  *libaray;
                *libaray = book;
. . . . . .
                *library = book;   //library为空,直接指向新的节点

}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-4 05:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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