3 回答

TA贡献1836条经验 获得超13个赞
1.int StackEmpty(*S){ //此处好象编译出错!!!
S没声明啊.
2.int InOrderTraverse(BiTree T) {
stack S;
你定义的栈类名字是SqStack
3.你的Push,Pop声明的第二个参数是int,int *型,但main中调用时传的是BiTree型.

TA贡献1784条经验 获得超7个赞
//好像算法有问题..死循环了.
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "stdlib.h"//要加上这个头文件,因为你用了exit()函数
#include <malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define ERROR 0
#define OK 1
#define OVERFLOW -2
typedef struct BiTNode{
int data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct {
int *base;
int *top;
int stacksize;
} SqStack;
int InitStack (SqStack *S)
{
S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if (!S->base)
exit (OVERFLOW);
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
return OK;
}
int StackEmpty(SqStack *S){ //此处好象编译出错!!!少了类型了.SqStack
if(S->top==S->base)
return OK;
else return ERROR;
}
int Push (SqStack *S,int e)
{
if (S->top - S->base >= S->stacksize)
{
S->base = (int *) realloc ( S->base,(S->stacksize + STACKINCREMENT) * sizeof (int));
if (!S->base)
exit (OVERFLOW);
S->top = S->base + S->stacksize;
S->stacksize += STACKINCREMENT;
}
*S->top++ = e;
return OK;
}
int Pop (SqStack *S, int *e)
{
if (S->top == S->base) return ERROR;
*e = *--S->top;
return OK;
}
BiTree CreateBiTree(BiTree T) {
int ch;
scanf("%d",&ch);
if (ch==0)
T = NULL;
else {
if (!(T = (BiTree)malloc(sizeof(BiTNode))))
return ERROR;
T->lchild=CreateBiTree(T->lchild);
T->data = ch;
T->rchild=CreateBiTree(T->rchild);
}
return T;
}
int InOrderTraverse(BiTree T) {
SqStack S; //应该是SqStack
BiTree p;
InitStack(&S); p = T;
while (p || !StackEmpty(&S)) {
if (p) {
Push(&S, p->data); //p改成p->data是把data进栈.
p = p->lchild;
}
else {
Pop(&S, &(p->data)); //p改成p->data是把data的地址赋.
if (p->data!=0) return ERROR;
p = p->rchild;
}
}
return OK;
}
void main()
{
BiTree T(0);
printf("chuang jian zhong xu er cha shu:\n");
T=CreateBiTree(T);
printf("bian li er cha shu :\n");
InOrderTraverse(T);
}

TA贡献1872条经验 获得超3个赞
为什么不用递归?
void InOrderTraverse(BiTree T)
{
if(T != NULL)
{
InOrderTraverse(T->lChild);
Visit(T); //访问T结点
InOrderTraverse(T->rChild);
}
}
添加回答
举报