鱼C论坛

 找回密码
 立即注册
查看: 3761|回复: 0

[学习笔记] 单链表

[复制链接]
发表于 2022-5-25 21:29:54 | 显示全部楼层 |阅读模式

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

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

x
听了小甲鱼两节单链表,在原本代码增加删除和修改函数
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void getInput(struct Book *book);//添加数据
  5. void addBook(struct Book **library);//定位指针
  6. void printLibrary(struct Book *library);//显示所有数据
  7. void printBook(struct Book *book);//显示查找数据
  8. void library_free(struct Book **library);//释放内存
  9. struct Book *searchBook(struct Book *library, char *target);//搜索数据
  10. void delBook(struct Book *book); //删除数据
  11. void reviseBook(struct Book *book);//修改数据
  12. struct Book *delsearchBook(struct Book *library, char *target);//搜索删除数据的前一个

  13. struct Book{
  14.         char number[30];
  15.         char title[128];
  16.         char author[40];
  17.         struct Book *next;
  18. };

  19. void getInput(struct Book *book)
  20. {
  21.         printf("请输入书号:");
  22.         scanf("%s", book->number);
  23.         printf("请输入书名:");
  24.         scanf("%s", book->title);
  25.         printf("请输入作者:");
  26.         scanf("%s", book->author);
  27.        
  28. }

  29. void addBook(struct Book **library)
  30. {
  31.         struct Book *book, *temp;
  32.                 static struct Book *tail;
  33.         book = (struct Book *)malloc(sizeof(struct Book));
  34.         if (book == NULL)
  35.         {
  36.                 printf("内存分配失败!\n");
  37.                 exit(1);
  38.         }

  39.         getInput(book);
  40.         if (*library != NULL)
  41.         {
  42.             /*temp = *library;
  43.             while(temp->next != NULL)
  44.                         {
  45.                                 temp = temp->next;
  46.                         }
  47.             temp->next = book;
  48.             book->next = NULL;*/
  49.             tail->next = book;
  50.             book->next= NULL;
  51.         }
  52.         else
  53.         {
  54.                 *library = book;
  55.                 book->next = NULL;
  56.                 }
  57.                 tail = book;
  58. }



  59. void printLibrary(struct Book *library)
  60. {
  61.         struct Book *book;
  62.         int count = 1;
  63.         book = library;
  64.         while(book != NULL)
  65.         {
  66.                 printf("book%d\n",count);
  67.                 printf("书号:%s\n", book->number);
  68.                 printf("书名:%s\n", book->title);
  69.                 printf("作者:%s\n", book->author);
  70.                 book = book->next;
  71.                 count++;
  72.         }
  73. }

  74. struct Book *searchBook(struct Book *library, char *target)
  75. {
  76.         struct Book *book;
  77.         book = library;
  78.         while(book != NULL)
  79.         {
  80.                 if(!strcmp(book->number, target))
  81.                 {
  82.                         break;
  83.                 }
  84.                 book = book->next;
  85.         }
  86.         return book;
  87. }

  88. void printBook(struct Book *book)
  89. {
  90.         printf("书号:%s\n", book->number);
  91.         printf("书名:%s\n", book->title);
  92.         printf("作者:%s\n", book->author);
  93. }

  94. struct Book *delsearchBook(struct Book *library, char *target)
  95. {
  96.         struct Book *book;
  97.         book = library;
  98.         if(book == NULL){
  99.                 printf("链表为空,删除失败");
  100.                 return book;
  101.         }
  102.        
  103.         while(book->next != NULL)
  104.         {
  105.                 if(!strcmp(book->next->number, target))
  106.                 {
  107.                         break;
  108.                 }
  109.                 book = book->next;
  110.                 book->next = book->next->next;
  111.         }
  112.         if(book->next == NULL){
  113.                 printf("没有这个数据,删除失败");
  114.                 return book;
  115.         }
  116.         return book;
  117. }

  118. void delBook(struct Book *book)
  119. {
  120.         struct Book *del;
  121.        
  122.         del = book->next;
  123.         book->next = del->next;
  124.         free(del);
  125.         printf("删除数据成功\n");
  126.        
  127. }

  128. void reviseBook(struct Book *book)
  129. {
  130.         char a1[128];
  131.         char a2[40];
  132.        
  133.         printf("书名修改为:");
  134.         scanf("%s",a1);
  135.         printf("作者修改为:");
  136.         scanf("%s",a2);
  137.         strcpy(book->title, a1);
  138.         strcpy(book->author, a2);
  139.        
  140.         printf("修改后\n");
  141.         printf("书号:%s\n", book->number);
  142.         printf("书名:%s\n", book->title);
  143.         printf("作者:%s\n", book->author);
  144. }



  145. void library_free(struct Book **library)
  146. {
  147.         struct Book *temp;
  148.         while(*library != NULL)
  149.         {
  150.                 temp = *library;
  151.                 *library = (*library)->next;
  152.                 free(temp);
  153.                
  154.         }
  155. }


  156. int main(void)
  157. {
  158.         struct Book *library = NULL;
  159.         struct Book *book;
  160.         struct Book *delbook;
  161.         int ch, a;
  162.         char target[30];
  163.        
  164.        
  165.         while(1)
  166.         {
  167.                 printf("1录入信息,2打印信息,3搜索数据,4删除数据,5修改数据,6退出   :");
  168.                 scanf("%d", &a);
  169.                 switch(a)
  170.                 {
  171.                         case 1 :
  172.                                 while(1)
  173.                                 {
  174.                                         printf("请问是否需要输入信息(Y/N):");
  175.                                         do
  176.                                         {
  177.                                                 ch = getchar();
  178.                                         }while(ch != 'Y' && ch != 'N');
  179.                                         if(ch == 'Y')
  180.                                         {
  181.                                                 addBook(&library);
  182.                                         }
  183.                                         else
  184.                                         {
  185.                                                 break;
  186.                                         }
  187.                                 }
  188.                         break;
  189.                         case 2 :
  190.                                 printLibrary(library);
  191.                                 /*printf("请问是否需要输出信息(Y/N):");
  192.                                 do
  193.                                 {
  194.                                         ch = getchar();
  195.                                 }while(ch != 'Y' && ch != 'N');
  196.                                 if(ch == 'Y')
  197.                                 {
  198.                                         printLibrary(library);
  199.                                 }*/
  200.                                 break;
  201.                         case 3 :
  202.                                 printf("请输入书号:");
  203.                                 scanf("%s", target);
  204.                                
  205.                                 book = searchBook(library, target);
  206.                                
  207.                                 if(book == NULL)
  208.                                 {
  209.                                         printf("很抱歉,没有找到这个数据\n");
  210.                                 }
  211.                                 else
  212.                                 {
  213.                                         printBook(book);
  214.                                 }
  215.                                 break;
  216.                         case 4 :
  217.                                 printf("请输入删除的数据的书号:");
  218.                                 scanf("%s",target);
  219.                                 book = delsearchBook(library, target);
  220.                                 delBook(book);
  221.                                 break;
  222.                         case 5:
  223.                                 printf("请输入修改的数据的书号:");
  224.                                 scanf("%s",target);
  225.                                 book = searchBook(library, target);
  226.                                 reviseBook(book);
  227.                                 break;
  228.                         case 6:
  229.                                 printf("再见!\n");
  230.                                 library_free(&library);
  231.                                  exit(1);
  232.                                           
  233.                 }
  234.        
  235.         }
  236.                
  237.         return 0;
  238. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-16 10:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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