二叉树创建问题,为什么输入一个字符后,一直输入‘#’都结束不了

pentichengfeng 2012-11-17 09:48:16
#include <stdio.h>
#include <malloc.h>
#define maxsize 100

typedef struct node
{
char data;
struct node *lchild,*rchild;


} *bitree;

bitree create()
{
bitree t;
char ch;
ch=getchar();
if(ch=='#')
t=NULL;
else
{
t=(struct node *)malloc(sizeof(struct node));
t->data=ch;
t->lchild=create();
t->rchild=create();
}
return t;
}
void preorder(bitree t)
{
if(t==NULL)
return ;
else{
printf("%d",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}

void main()
{
bitree t,p;
t=create();
preorder(t);
printf("...............\n");


}
...全文
749 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
pentichengfeng 2012-11-19
  • 打赏
  • 举报
回复
引用 7 楼 daiyier 的回复:
typedef struct node { char data; struct node *lchild,*rchild; } *bitree; bitree create() { bitree t; char ch; scanf("%c",&ch); getchar(); if(ch=='#') { t = NULL; return NULL; ……
pentichengfeng 2012-11-19
  • 打赏
  • 举报
回复
引用 4 楼 cnm_1314 的回复:
楼主的问题已解决! 首先说明您的问题是因为在您输入的一串数据中函数没有过滤掉空格,一楼说的对,之不过她函数用错了 首先您的程序在vc++中输不进去数据了 因为函数把间隔符空格也处理了并没有过滤 请看在您的程序加上标示: C/C++ code?123456789101112131415161718192021222324252627282930313233343……
受益匪浅啊,太感谢了!
pentichengfeng 2012-11-19
  • 打赏
  • 举报
回复
引用 5 楼 cnm_1314 的回复:
另外楼主你的遍历函数有问题 printf("%d",t->data); 应该为printf("%c",t->data);
嗯,一下子没改过来
●杨毅 2012-11-18
  • 打赏
  • 举报
回复
另外楼主 建议您最好去看看缓冲区的概念 在百度 scanf 与缓冲区 就明白了
●杨毅 2012-11-18
  • 打赏
  • 举报
回复
另外楼主你的遍历函数有问题 printf("%d",t->data); 应该为printf("%c",t->data);
●杨毅 2012-11-18
  • 打赏
  • 举报
回复
楼主的问题已解决! 首先说明您的问题是因为在您输入的一串数据中函数没有过滤掉空格,一楼说的对,之不过她函数用错了 首先您的程序在vc++中输不进去数据了 因为函数把间隔符空格也处理了并没有过滤 请看在您的程序加上标示:

#include <stdio.h>
#include <malloc.h>
#define maxsize 100
 
typedef struct node
{
    char data;
    struct node *lchild,*rchild;
 
 
} *bitree;
  
bitree create()
{
    bitree t;
    char ch;
    scanf("%c",&ch);
	printf("%c@\n",ch); //输入的数据 以@标示
    if(ch == '#')
	{
        t=NULL;
		printf("!!!!!!!!!!!!!!!!!!!\n");//if执行
	}
    else
    {
        t=(struct node *)malloc(sizeof(bitree));
        t->data=ch;
		printf("...%c\n",t->data);//else执行
        t->lchild=create();
        t->rchild=create();
    }
    return t;
}
void preorder(bitree t)
{
    if(t == NULL)
		return;
	else
    {
    printf("%c",t->data);
    preorder(t->lchild);
    preorder(t->rchild);
    }
}
 
void main()
{
    bitree t,p;
    t=create();
    preorder(t);
    printf("...............\n");
     
 
}//1 2 3 # # 4 5 # 6 # # 7 # # #

这样程序执行结果为: 1 2 3 # # 4 5 # 6 # # 7 # # # 1@ ...1 @ ... 2@ ...2 @ ... 3@ ...3 @ ... #@ !!!!!!!!!!!!!!!!!! @ ... #@ !!!!!!!!!!!!!!!!!! 其他省略 这样楼主是不是能看书来电倪端了?????? 是不是空格也处理了? 对吧! 现在在想想 既然空格也处理了那就过滤掉空格不就行了 所以在您scanf后面加上一句 getchar() 就完事了 源代码

#include <stdio.h>
#include <malloc.h>
#define maxsize 100
 
typedef struct node
{
    char data;
    struct node *lchild,*rchild;
 
 
} *bitree;
  
bitree create()
{
    bitree t;
    char ch;
    scanf("%c",&ch);
	getchar();
    if(ch == '#')
	{
        t=NULL;
	}
    else
    {
        t=(struct node *)malloc(sizeof(bitree));
        t->data=ch;
        t->lchild=create();
        t->rchild=create();
    }
    return t;
}
void preorder(bitree t)
{
    if(t == NULL)
		return;
	else
    {
    printf("%c",t->data);
    preorder(t->lchild);
    preorder(t->rchild);
    }
}
 
void main()
{
    bitree t,p;
    t=create();
    preorder(t);
    printf("...............\n");
     
 
}//1 2 3 # # 4 5 # 6 # # 7 # # #

特别说明 要是您的结构体里面是int型数据就不用加getchar了 因为scanf自动过滤掉掉整形数据的空格符
daiyier 2012-11-18
  • 打赏
  • 举报
回复
typedef struct node { char data; struct node *lchild,*rchild; } *bitree; bitree create() { bitree t; char ch; scanf("%c",&ch); getchar(); if(ch=='#') { t = NULL; return NULL; } else { t=(struct node *)malloc(sizeof(struct node)); t->data=ch; printf("输入%c的左孩子:",ch); t->lchild=create(); printf("输入%c的右孩子:",ch); t->rchild=create(); } return t; } void preorder(bitree t) { if(t==NULL) return ; else{ printf("%c",t->data); preorder(t->lchild); preorder(t->rchild); } } void main() { bitree t,p; printf("输入root:"); t=create(); preorder(t); printf("...............\n"); getchar(); } 因为楼主的数据类型是char型,所以在输入的时候要特别注意,如果data是整型,应该就捕获有那么多问题了。 这是我运行的结果:构建了一颗线序遍历为1 2 3的树。 输入root:1 输入1的左孩子:2 输入2的左孩子:# 输入2的右孩子:# 输入1的右孩子:3 输入3的左孩子:# 输入3的右孩子:# 123...............
pentichengfeng 2012-11-17
  • 打赏
  • 举报
回复
引用 1 楼 zhangxiaolei87 的回复:
因为你的输入有回车,所以结束不了。你把getchar改为getch不就就好了
输不进数
pentichengfeng 2012-11-17
  • 打赏
  • 举报
回复
引用 1 楼 zhangxiaolei87 的回复:
因为你的输入有回车,所以结束不了。你把getchar改为getch不就就好了
那个函数好像没看过呀,貌似也不行
zhangxiaolei87 2012-11-17
  • 打赏
  • 举报
回复
因为你的输入有回车,所以结束不了。你把getchar改为getch不就就好了

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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