您的位置:首页 > 编程语言 > C语言/C++

关于软件考试的一道C语言的解析

2011-05-03 12:42 375 查看
对于2009年上半年软件设计师下午试题的一个分析,对于第五个空很多网上给出的答案都是ptr->rchild,本人认为应为q->elem->rchild,因为此时ptr已为空,ptr->rchild会报错,以下为本人写的测试程序,在c++ builder中测试通过

//---------------------------------------------------------------------------

#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
#define NULL 0
typedef struct BtNode{
char data;
struct BtNode *lchild,*rchild;
}BiTNode,*BtTree;

typedef struct StNode{
BtTree elem;
struct StNode *link;
}StNode;

BtTree createBitTree()
{ char ch;
BtTree bt;
scanf("%c",&ch);

if(ch==' ') bt=NULL;
else
{if (ch!='#')
{ bt=(BtTree *)malloc(sizeof(BiTNode));
bt->data=ch;
bt->lchild=createBitTree();
bt->rchild=createBitTree();
}
else
return(bt);}
return(bt);
}

void visit(StNode *q)
{
printf(" %c ",q->elem->data);
}
int InOrder(BtTree root)
{
BtTree ptr;
StNode *q;
StNode *stacktop=NULL;
ptr=root;
while (ptr!=NULL || stacktop!=NULL) {
while (ptr!=NULL) {
q=(StNode *)malloc(sizeof(StNode));
if (q==NULL)
return -1;
q->elem=ptr;
q->link=stacktop;
stacktop=q;
ptr=ptr->lchild;
}
q=stacktop;
stacktop=q->link; //或为stacktop=stacktop->link;
visit(q);
ptr=q->elem->rchild; //如为ptr=ptr->rchild 运行报错
free(q);
}
return 0;
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/djcsch2001/archive/2009/10/17/4689093.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: