鱼C论坛

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

[已解决]搜索链表,打印不对,输一个名字,其他的都跟着出来了

[复制链接]
发表于 2021-1-2 15:08:34 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<winuser.inl>
  2. #include<stdio.h>
  3. #include <cstdlib>
  4. #include<string.h>
  5. //输入学生以及每个学生的姓名成绩
  6. //输入学生姓名,显示成绩,
  7. //用头插法向链表里面添加元素
  8. void getInput(struct Student* stu);
  9. void printfInfo(struct Student* head);
  10. void releaseMemory(struct Student** head);
  11. void head_add(struct Student** head);

  12. struct Student
  13. {
  14.         char name[20];
  15.         int score;
  16.         struct Student* next;
  17. };

  18. void getInput(struct Student* stu)
  19. {
  20.         printf("name:");
  21.     scanf_s("%s", stu->name, 20);
  22.         printf("score:");
  23.         scanf_s("%d", &stu->score);
  24. }

  25. void printfInfo(struct Student *head)
  26. {
  27.         struct Student* stu;
  28.         stu = head;
  29.         while (stu != NULL)
  30.         {
  31.                 printf("name: %s \n", stu->name);
  32.                 printf("score:%d \n", stu->score);
  33.                 stu = stu->next;
  34.         }
  35. }

  36. void search_stu(struct Student** head, char name[])
  37. {
  38.         struct Student* stu, * temp;
  39.         stu = *head;
  40.         int sign = 0;
  41.         if (stu == NULL)
  42.         {
  43.                 printf("there is no student!\n");
  44.         }
  45.         else
  46.         {
  47.                 while (stu != NULL & sign != 1)
  48.                 {
  49.                         if (!strcmp(stu->name, name))
  50.                         {
  51.                                 printfInfo(stu);
  52.                                 sign = 1;
  53.                                 break;
  54.                         }
  55.                         else
  56.                         {
  57.                                 stu = stu->next;
  58.                         }
  59.                 };
  60.                 if (sign == 0)
  61.                 {
  62.                         printf("not found!\n");
  63.                 }
  64.         }

  65.        
  66. }

  67. void releaseMemory(struct Student** head)
  68. {
  69.         struct Student* stu, * temp;
  70.         stu = *head;
  71.         while (stu != NULL)
  72.         {
  73.                 temp = stu;
  74.                 stu = stu->next;
  75.                 free(temp);
  76.         }
  77. }

  78. void head_add(struct Student** head)
  79. {
  80.         struct Student* stu, * temp;

  81.         stu = (struct Student*)(malloc(sizeof(struct Student)));
  82.         if (stu == NULL)
  83.         {
  84.                 printf("memory failed");
  85.                 exit(1);
  86.         }
  87.         getInput(stu);

  88.         if (*head != NULL)  //不是空链表
  89.         {
  90.                 temp = *head;
  91.                 *head = stu;
  92.                 stu->next = temp;
  93.         }
  94.         else
  95.         {
  96.                 *head = stu;
  97.                 stu->next = NULL;
  98.         }
  99. }

  100. int main()
  101. {
  102.         struct Student* namelist = NULL;  ////初始化一个指针,头指针,相当于定义了一个空的单链表
  103.         //struct Student* stu = NULL;

  104.         int num;
  105.         printf("total num of stu:");
  106.         scanf_s("%d", &num);
  107.         printf("\n");

  108.         printf("enter info:\n");
  109.     for (int i = 0; i < num; i++)
  110.         {
  111.                 head_add(&namelist);
  112.         }
  113.         printf("finish entering!\n");

  114.         //printf("begining to print\n");
  115.         //printfInfo(&namelist);

  116.         char name[20];
  117.         do
  118.         {
  119.                 printf("enter the name you want to search:('#'表示结束)");
  120.                 scanf_s("%s",name,20);
  121.                 search_stu(&namelist, name);
  122.         } while (name[0] != '#');


  123.         releaseMemory(&namelist);
  124.         return 0;

  125. }
复制代码
最佳答案
2021-1-2 15:20:10
本帖最后由 jackz007 于 2021-1-2 15:27 编辑
  1. void search_stu(struct Student** head, char name[])
  2. . . . . . .
  3.                 while (stu != NULL & sign != 1)    // 错误
  4.                 {
  5.                         if (!strcmp(stu->name, name))
  6.                         {
  7.                                 printfInfo(stu);   // 错误
复制代码

        改为
  1. void search_stu(struct Student** head, char name[])
  2. . . . . . .
  3.                 while (stu != NULL && sign != 1)                    // 修改
  4.                 {
  5.                         if (!strcmp(stu->name, name))
  6.                         {
  7.                                 printf("name: %s \n", stu->name)  ; // 修改
  8.                                 printf("score:%d \n", stu->score) ; // 修改
复制代码

        此外
  1.                 printf("enter the name you want to search:('#'表示结束)");
  2.                 fflush(stdin)                           ; // 添加此句,清除键盘缓冲区中滞留的 '\n' 字符,否则,后面的 name[0] != '#' 判断极有可能失效
  3.                 scanf("%s",name);
复制代码

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

使用道具 举报

发表于 2021-1-2 15:20:10 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-1-2 15:27 编辑
  1. void search_stu(struct Student** head, char name[])
  2. . . . . . .
  3.                 while (stu != NULL & sign != 1)    // 错误
  4.                 {
  5.                         if (!strcmp(stu->name, name))
  6.                         {
  7.                                 printfInfo(stu);   // 错误
复制代码

        改为
  1. void search_stu(struct Student** head, char name[])
  2. . . . . . .
  3.                 while (stu != NULL && sign != 1)                    // 修改
  4.                 {
  5.                         if (!strcmp(stu->name, name))
  6.                         {
  7.                                 printf("name: %s \n", stu->name)  ; // 修改
  8.                                 printf("score:%d \n", stu->score) ; // 修改
复制代码

        此外
  1.                 printf("enter the name you want to search:('#'表示结束)");
  2.                 fflush(stdin)                           ; // 添加此句,清除键盘缓冲区中滞留的 '\n' 字符,否则,后面的 name[0] != '#' 判断极有可能失效
  3.                 scanf("%s",name);
复制代码

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-3 16:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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