|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本意是想先输入一个数字表示字符串长度,再输入字符串,将字符串内的字符依次输出
- #include <stdio.h>
- #include <stdlib.h>
- struct node
- {
- char zf;
- struct node *next;
- };
- int main()
- {
- int n;
- scanf("%d",&n);
- struct node *head,*rear,*p;
- head=(struct node*)malloc(sizeof(struct node));
- head->next=NULL;
- rear=head;
- for(int i=0;i<n;i++)
- {
- p=(struct node*)malloc(sizeof(struct node));
- scanf("%c",&p->zf);
- rear->next=p;
- rear=p;
- }
- rear->next=NULL;
- p=head->next;
- while(p)
- {
- printf("%c ",p->zf);
- p=p->next;
- }
- }
复制代码
运行之后假设输入“4 asdf”输出的结果是“ a s d”
但是将结构体中的char类型换成int类型之后输入“4 1 2 3 4”输出的结果是“1 2 3 4”
应该是第一次取n的时候,缓冲区里还有一个回车符
可以用 getchar() 吃掉回车符
- #include <stdio.h>
- #include <stdlib.h>
- struct node
- {
- char zf;
- struct node *next;
- };
- int main()
- {
- int n;
- scanf("%d",&n);
- getchar();
- struct node *head,*rear,*p;
- head=(struct node*)malloc(sizeof(struct node));
- head->next=NULL;
- rear=head;
- for(int i=0;i<n;i++)
- {
- p=(struct node*)malloc(sizeof(struct node));
- scanf("%c",&p->zf);
- rear->next=p;
- rear=p;
- }
- rear->next=NULL;
- p=head->next;
- while(p)
- {
- printf("%c ",p->zf);
- p=p->next;
- }
- }
复制代码
|
|