C语言的一个问题(可能有关编译器的行为,高手请进)

来源:百度知道 编辑:UC知道 时间:2024/07/04 06:25:19
我在练习C程序时出现这样一个问题:
#include <iostream.h>

struct bitree
{
int data;
bitree *lchild;
bitree *rchild;
};

void preorder(bitree *root);
void postorder(bitree *root);
bitree *createtree();

bitree *createtree()
{
int a;
cin >> a;
if( a != 0 )
{
bitree *q = new bitree;
q -> data = a;
q -> lchild = createtree();
q -> rchild = createtree();
return q;
}
else
return NULL;
}

inline void preorder( bitree *root )
{
if( root != NULL )
{
cout << root -> data << " ";
preorder( root -> lchild );
preorder( root -> rchild );
}
}

inline void postorder( bitree *root )
{
if( root != NULL )
{
postorder( root -> lchild );
postorder( root -> rchild );
cout << root -> data <

我在跑这个程序时也遇到了同样的问题,程序是先执行的while语句然后再输出的。但在调试时程序的确是按顺序执行的!
而且,我还发现在debug下即使去掉while语句,也不能输出结果。
调试到二叉树前序和后序遍历的函数中的输出语句时程序正常执行,root -> data的值都是正确的,但就是没有输出!真是奇哉怪也!

网上搜了一下调试和运行时得到的结果不一样的问题,但都是在debug下可以得到正确结果,运行时出错。像这个在调试时出问题,运行正确的情况还真是罕见。百思不得其解!

抱歉没有能帮你解决问题。