鱼C论坛

 找回密码
 立即注册
查看: 981|回复: 2

[已解决]关于链表的字符输入输出疑惑

[复制链接]
发表于 2022-2-25 17:30:38 | 显示全部楼层 |阅读模式

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

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

x
本意是想先输入一个数字表示字符串长度,再输入字符串,将字符串内的字符依次输出
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct node
  4. {
  5.     char zf;
  6.     struct node *next;
  7. };

  8. int main()
  9. {
  10.     int n;
  11.     scanf("%d",&n);
  12.     struct node *head,*rear,*p;
  13.     head=(struct node*)malloc(sizeof(struct node));
  14.     head->next=NULL;
  15.     rear=head;
  16.     for(int i=0;i<n;i++)
  17.     {
  18.         p=(struct node*)malloc(sizeof(struct node));
  19.         scanf("%c",&p->zf);
  20.         rear->next=p;
  21.         rear=p;
  22.      }
  23.     rear->next=NULL;
  24.     p=head->next;
  25.     while(p)
  26.         {
  27.                    printf("%c ",p->zf);
  28.                     p=p->next;
  29.         }
  30. }
复制代码

运行之后假设输入“4  asdf”输出的结果是“ a s d”
但是将结构体中的char类型换成int类型之后输入“4     1 2 3 4”输出的结果是“1 2 3 4”
最佳答案
2022-2-25 21:03:03
应该是第一次取n的时候,缓冲区里还有一个回车符

可以用 getchar() 吃掉回车符

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct node
  4. {
  5.     char zf;
  6.     struct node *next;
  7. };

  8. int main()
  9. {
  10.     int n;
  11.     scanf("%d",&n);
  12.     getchar();
  13.     struct node *head,*rear,*p;
  14.     head=(struct node*)malloc(sizeof(struct node));
  15.     head->next=NULL;
  16.     rear=head;
  17.     for(int i=0;i<n;i++)
  18.     {
  19.         p=(struct node*)malloc(sizeof(struct node));
  20.         scanf("%c",&p->zf);
  21.         rear->next=p;
  22.         rear=p;
  23.      }
  24.     rear->next=NULL;
  25.     p=head->next;
  26.     while(p)
  27.         {
  28.                    printf("%c ",p->zf);
  29.                     p=p->next;
  30.         }
  31. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-25 19:25:17 | 显示全部楼层
你的代码,看看这句 printf("%c ",p->zf);

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct node
  4. {
  5.     char zf;
  6.     struct node *next;
  7. };

  8. int main()
  9. {
  10.     int n;
  11.     scanf("%d",&n);
  12.     getchar(); // 忽略空格

  13.     struct node *head, *p, *tmp;

  14.     for(int i=0; i<n; i++)
  15.     {
  16.         tmp=(struct node*)malloc(sizeof(struct node));
  17.         tmp->next=NULL;
  18.         scanf("%c",&tmp->zf);

  19.         // 保存头指针
  20.         if(i==0)        {
  21.             p = tmp;
  22.             head = tmp;
  23.             continue;
  24.         }

  25.         // 交换指针
  26.         p->next=tmp;
  27.         p=tmp;
  28.      }

  29.     p=head;
  30.     while(p)
  31.     {
  32.         printf("%c",p->zf);
  33.         p=p->next;
  34.     }

  35.     return 0;

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

使用道具 举报

发表于 2022-2-25 21:03:03 | 显示全部楼层    本楼为最佳答案   
应该是第一次取n的时候,缓冲区里还有一个回车符

可以用 getchar() 吃掉回车符

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct node
  4. {
  5.     char zf;
  6.     struct node *next;
  7. };

  8. int main()
  9. {
  10.     int n;
  11.     scanf("%d",&n);
  12.     getchar();
  13.     struct node *head,*rear,*p;
  14.     head=(struct node*)malloc(sizeof(struct node));
  15.     head->next=NULL;
  16.     rear=head;
  17.     for(int i=0;i<n;i++)
  18.     {
  19.         p=(struct node*)malloc(sizeof(struct node));
  20.         scanf("%c",&p->zf);
  21.         rear->next=p;
  22.         rear=p;
  23.      }
  24.     rear->next=NULL;
  25.     p=head->next;
  26.     while(p)
  27.         {
  28.                    printf("%c ",p->zf);
  29.                     p=p->next;
  30.         }
  31. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 01:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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