|
|

楼主 |
发表于 2005-7-25 14:08:37
|
显示全部楼层
getchar不是只能接受一个字符吗?
我把getchar接受到的字符赋给了x->data,
输入每个字符后回车,
然后输出,
可是把字符连同回车一块输出了,
这是怎么回事?
- #include <stdio.h>
- #include <malloc.h>
- typedef struct linklist {
- char data;
- struct linklist *next;
- };
- void main () {
- char ch;
- struct linklist *head, *p, *r, *x;
- p=(struct linklist *) malloc (sizeof(struct linklist));
- head=p;
- (*head).next=NULL;
- r=head;
- ch=getchar ();
- while (ch!='?') {/* '?'是结束标志字符.如abc? */
- p=(struct linklist *) malloc (sizeof(struct linklist));
- p->data=ch;
- (*r).next=p;
- r=p;
- ch=getchar ();
- }
- if (r!=head)
- r->next=NULL;
- ch=getchar ();
- x=(*head).next;
- printf ("the list is:\n");
- while (x!=NULL) {
- printf ("%d", x->data);
- x=x->next;
- }
- }
复制代码 |
|