鱼C论坛

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

单链表第二节

[复制链接]
发表于 2020-9-27 11:09:10 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct Book                        //初始化结构体
  4. {
  5.     char tittle[128];
  6.     char author[24];
  7.     struct Book *next;
  8. };
  9. getInput(struct Book* book)
  10. {
  11.     printf("请输入书名:\n");
  12.     scanf("%s", book->tittle);
  13.     printf("请输入作者:\n");
  14.     scanf("%s", book->author);
  15. }
  16. addBook(struct Book** head)          //向结构体中插入节点
  17. {
  18.     struct Book *book;
  19.     static struct Book *tail;=============================问题:即使一开始空链表执行else,但还是不明白tail是如何定位到链表尾部的,
  20.     book = (struct Book *)malloc(sizeof(struct Book));//创建新节点
  21.     if(book==NULL)
  22.     {
  23.         printf("节点创建失败,退出程序!!!");
  24.         exit(1);
  25.     }
  26.     getInput(book);                 //录入书的内容
  27.     if(*head!=NULL)
  28.     {
  29.         tail->next = book;
  30.         book->next = NULL;
  31.     }
  32.     else
  33.     {
  34.         *head = book;
  35.         book->next= NULL;
  36.     }
  37. }
  38. void Print (struct Book* head)            //打印链表内容
  39. {
  40.     struct Book *book;
  41.     int count = 1;                   //计数君count
  42.     book = head;
  43.     while(book!=NULL)
  44.     {
  45.         printf("第%d本书\n", count);
  46.         printf("书名:%s\n",book->tittle);
  47.         printf("作者:%s\n",book->author);
  48.         book = book->next;
  49.         count++;
  50.     }

  51. }
  52. Free(struct Book** head)            //每次释放的都是首个节点
  53. {
  54.     struct Book *temp;
  55.     while(*head!=NULL)
  56.     {
  57.         temp = *head;
  58.         
  59.         *head = (*head)->next;
  60.         free(temp);
  61.     }
  62. }

  63. int main()
  64. {
  65.     struct Book *head = NULL;
  66.     char ch;
  67.     while (1)
  68.         {
  69.                 printf("请问是否需要录入书籍信息(y / n): ");
  70.                 do
  71.                 {
  72.                         ch = getchar();
  73.                 } while (ch != 'y' && ch != 'n');
  74.                   
  75.                 if (ch == 'y')
  76.                 {
  77.                         addBook(&head);    //传递的参数是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.                 Print(head);        //打印library指向的链表信息
  92.         }
  93.          

  94.         Free(head);         //释放library指向的链表内存

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

使用道具 举报

发表于 2020-9-27 14:18:03 | 显示全部楼层
addBook 函数有问题


  1. void addBook(struct Book** head)          //向结构体中插入节点
  2. {
  3.     struct Book *book;
  4.     //tail定义的是静态变量,每次进入函数时,tail会保持原有的值。
  5.     static struct Book *tail;
  6.     book = (struct Book *)malloc(sizeof(struct Book));//创建新节点
  7.     if(book==NULL)
  8.     {
  9.         printf("节点创建失败,退出程序!!!");
  10.         exit(1);
  11.     }
  12.     getInput(book);                 //录入书的内容
  13.     if(*head!=NULL)
  14.     {
  15.         tail->next = book;
  16.         book->next = NULL;
  17.     }
  18.     else
  19.     {
  20.         *head = book;
  21.         book->next= NULL;
  22.     }
  23.     //问题在这里,需要将新增节点的值赋给tail
  24.     tail = book;
  25. }
复制代码


主函数的Free(head)调用错误,按你定义的函数参数应该是这样写
  1. Free(&head);
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-13 10:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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