鱼C论坛

 找回密码
 立即注册
查看: 2431|回复: 4

单链表

[复制链接]
发表于 2021-5-3 22:22:07 | 显示全部楼层 |阅读模式

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

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

x
怎么程序只能运行个开头?


  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. {
  11.         printf("请输入书名:");
  12.         scanf("%s",book->title);
  13.         printf("请输入作者:");
  14.         scanf("%s",book->author);
  15. }
  16. void addBook(struct Book **library)
  17. {
  18.         struct Book *book,*temp;
  19.         book=(struct Book *)malloc (sizeof(struct Book));
  20.         if(book=NULL)
  21.         {
  22.                 printf("内存分配失败!\n");
  23.                 exit(1);
  24.          }
  25.          getInput(book);
  26.          if(*library !=NULL)
  27.          {
  28.          temp=*library;
  29.          *library=book;
  30.          book->next=temp;
  31.          }
  32.          else
  33.          {
  34.                  *library=book;
  35.                  book->next=NULL;
  36.          }
  37. }
  38. void printLibrary(struct Book *library)
  39. {
  40.         struct Book *book;
  41.         int count =1;
  42.         book=library;
  43.         while(book!=NULL)
  44.         {
  45.                 printf("Book%d:",count);
  46.                 printf("书名:%s",book->title);
  47.                 printf("作者:%s",book->author);
  48.                 book=book->next;
  49.                 count++;
  50.          }
  51. }
  52. void releaseLibrary(struct Book *library)
  53. {

  54. while(library !=NULL)
  55. {
  56.   free(library);       
  57.   library=library->next;
  58.        
  59.        
  60. }
  61. }
  62. int main(void)
  63. {

  64. struct Book *library=NULL;
  65. int ch;
  66. while(1)
  67. {
  68.         printf("请问是否需要录入书籍信息(Y/N):");
  69.         do
  70.         {
  71.                 ch=getchar();
  72.                
  73.          }while(ch!='Y'&&ch!='N');
  74.          if(ch=='Y')
  75.          {
  76.                  addBook(&library);
  77.          }
  78.          else
  79.          {
  80.                  break;
  81.          }
  82. }
  83. printf("请问是否需要打印图书信息(Y/N):");
  84. do
  85. {
  86.         ch=getchar();
  87.           }while(ch!='Y'&&ch!='N');
  88.           if(ch=='Y')
  89.           {
  90.                   printLibrary(library);
  91.                  
  92.           }
  93.           releaseLibrary(library);
  94. return 0;       
  95. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-3 23:01:53 | 显示全部楼层
         if(book==NULL)
         {
                 printf("内存分配失败!\n");
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-4 11:34:04 | 显示全部楼层
ba21 发表于 2021-5-3 23:01
if(book==NULL)
         {
                 printf("内存分配失败!\n");

if里面的条件错了这个if就不执行了,可是这个if里面的程序能对整个程序有影响么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-4 23:10:28 | 显示全部楼层
认真学好C语言 发表于 2021-5-4 11:34
if里面的条件错了这个if就不执行了,可是这个if里面的程序能对整个程序有影响么?

请问你能给NULL对象 赋值吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-5 10:08:53 | 显示全部楼层
ba21 发表于 2021-5-4 23:10
请问你能给NULL对象 赋值吗?

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-29 23:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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