鱼C论坛

 找回密码
 立即注册
查看: 1102|回复: 5

[已解决]关于链表的一个问题

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

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

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

x
对于上次的帖子忘贴代码感到十分羞愧 这次重新发一个帖子 希望路过的大佬能帮帮我 程序使用vs2019写的


  1. void input_print()     //输入与输出的函数
  2. {
  3.         struct node* p1, * p2, * head;
  4.         int n, temp;
  5.         char temp_2;
  6.         p1 = p2 = (struct node*)malloc(sizeof(struct node));

  7.         printf("Plese input the book name:");
  8.         scanf("%s", &p1->name);
  9.         printf("Plese input the book author:");
  10.         scanf("%s", &p1->author);

  11.         head = NULL;
  12.         n = 0;

  13.         while (1)
  14.         {
  15.                 n++;

  16.                 if (n == 1)
  17.                 {
  18.                         head = p1;
  19.                 }
  20.                 else
  21.                 {
  22.                         p2->next = p1;
  23.                 }
  24.                 p2 = p1;

  25.                 p1 = (struct node*)malloc(sizeof(struct node));

  26.                 printf("Do you want to input information again? Y/N");
  27.                 scanf("%s", &temp_2);
  28.                 if (temp_2 == 'n' || temp_2 == 'N')
  29.                 {
  30.                         break;
  31.                 }


  32.                 printf("Plese input the book name:");
  33.                 scanf("%s", &p1->name);
  34.                 printf("Plese input the book author:");
  35.                 scanf("%s", &p1->author);

  36.         }
  37.         p1 = NULL;

  38.         while (head != NULL)
  39.         {

  40.                
  41.                 for (temp = 1; temp <= n; temp++)
  42.                 {
  43.                         printf("第%d本书", temp);
  44.                         printf("书名:《%s》\n", head->name);
  45.                         printf("作者: %s", head->author);
  46.                         printf("\n \n");
  47.                        
  48.                         head = head->next;
  49.                 }
  50.                
  51.                 head = NULL;
  52.         }
  53.        
  54. }     //这里出现"Run-Time Check Failure #2 - Stack around the variable 'temp_2' was corrupted."错误提示
复制代码


        这是node结构体的内容
  1. struct node      
  2. {
  3.         char name[50];
  4.         char author[50];
  5.         struct node* next;
  6. };
复制代码


最佳答案
2021-3-15 23:31:46
本帖最后由 jackz007 于 2021-3-15 23:33 编辑
  1.         char temp_2;
  2. . . . . . .
  3.         while (1)
  4.         {
  5. . . . . . .
  6.                 scanf("%s", & temp_2)  ; // char 型的变量不可以这样获取键盘输入
复制代码

        正确的方法
  1.                 fflush(stdin)          ; // 添加此句,读取单个字符之前,需要清空键盘缓冲区
  2.                 scanf("%c" , & temp_2) ; // char 型的变量应该这样获取键盘输入
复制代码

        也就是说,把原来的 scanf() 那一句改成上面的那两句就可以了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-15 23:23:03 From FishC Mobile | 显示全部楼层
本帖最后由 shiwobuhaoma 于 2021-3-15 23:42 编辑

光申请内存了,没有释放内存操作,还有如果加上申请内存后检查是否为空会更严谨一点。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-15 23:24:19 From FishC Mobile | 显示全部楼层
本帖最后由 shiwobuhaoma 于 2021-3-15 23:42 编辑

1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-15 23:25:55 From FishC Mobile | 显示全部楼层
temp2是char类型,你用%s不对吧,应该%c
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-15 23:31:46 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-3-15 23:33 编辑
  1.         char temp_2;
  2. . . . . . .
  3.         while (1)
  4.         {
  5. . . . . . .
  6.                 scanf("%s", & temp_2)  ; // char 型的变量不可以这样获取键盘输入
复制代码

        正确的方法
  1.                 fflush(stdin)          ; // 添加此句,读取单个字符之前,需要清空键盘缓冲区
  2.                 scanf("%c" , & temp_2) ; // char 型的变量应该这样获取键盘输入
复制代码

        也就是说,把原来的 scanf() 那一句改成上面的那两句就可以了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-16 12:55:53 | 显示全部楼层
啊这 原来是%c啊 一直以为是%s 误记了 谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 23:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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