这个C程序怎么结束键盘输入啊???

Bruce_Wang1991 2013-07-04 08:26:15
/* 这个程序给出了与二叉链表相关的一些操作
* 包括二叉树的建立
* 二叉树的前序、中序、后序遍历
*/

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

/* 二叉链表的结点结构 */
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;

/* 对主函数中即将被调用的函数进行声明 */
void CreateBiTree(BiTree tree);
void PreOrderTraverse(BiTree tree);
void InOrderTraverse(BiTree tree);
void PostOrderTraverse(BiTree tree);

int main(void)
{
BiTree tree;

/* 建立二叉树 */
CreateBiTree(tree);

/* 二叉树的前序遍历 */
PreOrderTraverse(tree);
printf("\n");

/* 二叉树的中序遍历 */
InOrderTraverse(tree);
printf("\n");

/* 二叉树的后序遍历 */
PostOrderTraverse(tree);
printf("\n");

return 0;
}

/* 递归的前序遍历的方法建立二叉树 */
void CreateBiTree(BiTree tree)
{
TElemType ch;
int i;
printf("Enter the data of the node: ");
do
{
scanf("%c", &ch);
} while(ch == '\n');

if(ch == '#')
tree = NULL;
else
{
tree = (BiTree) malloc (sizeof(BiTNode));
if (!tree)
{
printf("OVERFLOW !\n");
exit(1);
}
tree->data = ch;
CreateBiTree(tree->lchild);
CreateBiTree(tree->rchild);
}
}

/* 递归实现二叉树的前序遍历 */
void PreOrderTraverse(BiTree tree)
{
if (tree == NULL)
return;

printf("%3c", tree->data);
PreOrderTraverse(tree->lchild);
PreOrderTraverse(tree->rchild);
}

/* 递归实现二叉树的中序遍历 */
void InOrderTraverse(BiTree tree)
{
if (tree == NULL)
return;

InOrderTraverse(tree->lchild);
printf("%3c", tree->data);
InOrderTraverse(tree->rchild);
}

/* 递归实现二叉树的后序遍历 */
void PostOrderTraverse(BiTree tree)
{
if (tree == NULL)
return;

PostOrderTraverse(tree->lchild);
PostOrderTraverse(tree->rchild);
printf("%3c", tree->data);
}


键盘输入CTRL + d 根本不能结束键盘输入!还是会出现:Enter the data of the node:
还有什么其他的方法?求指教
...全文
210 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
橡木疙瘩 2013-07-04
  • 打赏
  • 举报
回复
ch == '#'是唯一回归条件,没有别的办法。 而且我怀疑这段代码是否能完成目的:
void CreateBiTree(BiTree tree)//tree是个值参
{
……
        tree = (BiTree) malloc (sizeof(BiTNode));//malloc分配的内存块如何交到父节点手中?
Bruce_Wang1991 2013-07-04
  • 打赏
  • 举报
回复
引用 3 楼 u010936098 的回复:
CreateBiTree是递归函数,要想知道如何结束,就去找回归条件,也就是不再调用自身的分支。 很明显,递归调用发生在if(ch == '#')判断的else分支中,那么要想结束递归就去满足这个条件判断,让ch == '#'成立。
这样是不行的!我试了!#号只是用来把二叉树变成扩展二叉树以使前序遍历建立的二叉树唯一确定
Lyinsc 2013-07-04
  • 打赏
  • 举报
回复
引用 1 楼 szxy5629ly 的回复:
do { scanf("%c", &ch); } while(ch == '\n'); 应该改成 do { scanf("%c", &ch); } while(ch != '\n');
对不起,我看错了。 楼上正解,连续多输入几个'#'可以结束
Bruce_Wang1991 2013-07-04
  • 打赏
  • 举报
回复
引用 2 楼 u010936098 的回复:
    if(ch == '#')
        tree = NULL;
输入#号
我试了,不是这样的,#号只是用来把二叉树变成扩展二叉树以求得前序遍历能唯一确定一棵二叉树的
图灵狗 2013-07-04
  • 打赏
  • 举报
回复
如果结束与程序逻辑相关,很明显你的代码是当输入'#'字符时,表示当前分支已经结束,这样当输入多个'#'字符使得所有分支都到末尾时,程序自然就结束了,估计你的代码不是自己写的,抄过来的吧。
橡木疙瘩 2013-07-04
  • 打赏
  • 举报
回复
CreateBiTree是递归函数,要想知道如何结束,就去找回归条件,也就是不再调用自身的分支。 很明显,递归调用发生在if(ch == '#')判断的else分支中,那么要想结束递归就去满足这个条件判断,让ch == '#'成立。
橡木疙瘩 2013-07-04
  • 打赏
  • 举报
回复
    if(ch == '#')
        tree = NULL;
输入#号
Lyinsc 2013-07-04
  • 打赏
  • 举报
回复
do { scanf("%c", &ch); } while(ch == '\n'); 应该改成 do { scanf("%c", &ch); } while(ch != '\n');

69,371

社区成员

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

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