关于头插法建立LinkList的问题

一剑 2007-11-28 03:54:49

#include <stdio.h>
#include <malloc.h>

typedef char datatype;
typedef struct node
{
datatype data;
struct node* next;
}linklist;

int main(int argc, char* argv[])
{
char ch;
linklist *head, *s;
head = NULL;
ch = getchar();
while(ch!='$')
{
s = (struct node*)malloc(sizeof(linklist));
s->data =ch;
s->next = head;
head = s;
ch = getchar();
}

struct node* p = head;
int i = 1;
while(p)
{
printf("第%d个节点数据为:%c\n", i, p->data);
p = p->next;
i++;
}
getchar();
return 0;
}


以上代码运行结果:
3
2
1
$
第1个节点数据为:

第2个节点数据为:1
第3个节点数据为:

第4个节点数据为:2
第5个节点数据为:

第6个节点数据为:3
Press any key to continue

明明只插入了3个节点啊,结果为什么会出现6个呢?
...全文
122 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
一剑 2007-11-28
  • 打赏
  • 举报
回复
123$
第1个节点数据为:3
第2个节点数据为:2
第3个节点数据为:1
Press any key to continue

OK
谢谢
hai040 2007-11-28
  • 打赏
  • 举报
回复
回车,试试3 2 1 $\n

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧