关于软件考试的一道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
//---------------------------------------------------------------------------
#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
相关文章推荐
- 关于软件考试的一道C语言的解析
- C语言中关于.h文件和.c文件详细解析
- [C语言]关于指针和int型的一道题目
- 跪求一道关于c语言和数据结构的问题
- c语言学习软件选择解析
- (C语言)关于位段空间的使用情况的经典面试题解析
- 关于定义顺序和内存分配的关系--记一道不严谨的C语言题
- 一道关于java 类初始化 成员初始化的笔试题的解析
- <志良电子>关于C语言中用Keil软件制作Lib库文件的几点经验
- 关于全国计算机技术与软件 专业技术资格考试推迟的紧急公告
- 关于键盘输入、缓冲输入、读入字符、解析C语言编程对缓冲区的理解
- 华为校招软件上机考试题目解析
- 关于明天的考试(oracle+软件项目管理)
- C语言中关于 赋值语句X=Y的解析
- 关于c语言的一道题目,适合新手看哦
- 关于C语言和C++语言的数据读取函数解析及简单的例子
- 关于俄罗斯方块游戏软件C语言初步感受
- 关于java类初始化过程的一道试题的解析
- 关于一道数据库例题的解析。为什么σ age>22 (πS_ID,SCORE (SC) ) 选项是错的?
- 一道关于内存的C语言题