主题:就是找不出错处,请大虾们帮忙看一下!!!
二叉树的中序遍历非递归算法,还有交换左右孩子的非递归算法.
运行后出现
错误 d:\苏\noname0.c 95: 类型不匹配在参数'e'在调用'Push' 在函数
错误 d:\苏\noname0.c 96: 类型不匹配在参数'e'在调用'pop' 在函数
错误 d:\苏\noname0.c 110: 类型不匹配在参数'e'在调用'Push' 在函数
错误 d:\苏\noname0.c 112: 类型不匹配在参数'e'在调用'pop' 在函数
不知道怎么改才好,所以向大虾们请教...
#define OK 1
#define ERROR 0
#define NULL 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
typedef struct BiTNode{
char data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
typedef char SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}Sqstack;
Sqstack *InitStack()
{ Sqstack *s;
s=(Sqstack*)malloc(sizeof(Sqstack));
s->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!s->base)exit(0);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return s;
}
StackEmpty(Sqstack *s)
{if(s==NULL) return 1;
else return 0;
}
int Push(Sqstack *s,SElemType e)
{if(s->top-s->base>=s->stacksize)
{ s->base=(SElemType*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof
(SElemType));
if(!s->base)exit(0);
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return OK;
}
int pop(Sqstack *s,SElemType e)
{if (s->top==s->base) return ERROR;
e=*--s->top;
return OK;
}
BiTNode *CreateBinTree()/*建立二叉树,用#代表空指针,参见P127构造二叉树操作*/
{BiTNode *t;
char x;
scanf("%c",&x);
if(x=='#')t=NULL;
else{
t=(BiTNode*)malloc(sizeof(BiTNode));
t->data=x;
t->lchild=CreateBinTree();
t->rchild=CreateBinTree();}
return(t);}
char visit(char x)
{printf("%c",x);
return OK;
}
char PreOrderTraverse(BiTree T,char (*visit)(char x))
{if(T){
if(visit(T->data))
if(PreOrderTraverse(T->lchild,visit))
if(PreOrderTraverse(T->rchild,visit)) return OK;
return ERROR;
}else return OK;
}
char PostOrderTraverse(BiTree T,char (*visit)(char x))
{if(T){
if(PreOrderTraverse(T->lchild,visit))
if(PreOrderTraverse(T->rchild,visit))
if(visit(T->data)) return OK;
return ERROR;
}else return OK;
}
char InOrderTraverse(BiTree T,char (*visit)(char x))
{Sqstack *s;BiTNode *p;SElemType e;
s=InitStack();
p=T;
while(p||!StackEmpty(s)){
if(p){Push(s,p);p=p->lchild;}
else{pop(s,p);if(!visit(p->data))return ERROR;
p=p->rchild;
}
}
return OK;
}
changlr(BiTree T)
{BiTNode *p;
BiTNode *t;
Sqstack *s;
s=InitStack();
p=T;
while(p||!StackEmpty(s))
{if(p){Push(s,p);p=p->lchild;}
else
{pop(s,p);
t=p->lchild;
p->lchild=p->rchild;
p->rchild=t;
p=p->lchild;
}
}return OK;
}
void main()
{char visit(char x);
BiTNode *r;
printf("create bintree\n");
r=CreateBinTree();
printf("root order search\n");
PreOrderTraverse(r,visit);
printf("\nmiddle order search\n");
InOrderTraverse(r,visit);
printf("\nlast order search\n");
PostOrderTraverse(r,visit);
changlr(r);
printf("\nbeing changed root order search\n");
PreOrderTraverse(r,visit);
}
运行后出现
错误 d:\苏\noname0.c 95: 类型不匹配在参数'e'在调用'Push' 在函数
错误 d:\苏\noname0.c 96: 类型不匹配在参数'e'在调用'pop' 在函数
错误 d:\苏\noname0.c 110: 类型不匹配在参数'e'在调用'Push' 在函数
错误 d:\苏\noname0.c 112: 类型不匹配在参数'e'在调用'pop' 在函数
不知道怎么改才好,所以向大虾们请教...
#define OK 1
#define ERROR 0
#define NULL 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
typedef struct BiTNode{
char data;
struct BiTNode *lchild;
struct BiTNode *rchild;
}BiTNode,*BiTree;
typedef char SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}Sqstack;
Sqstack *InitStack()
{ Sqstack *s;
s=(Sqstack*)malloc(sizeof(Sqstack));
s->base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!s->base)exit(0);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return s;
}
StackEmpty(Sqstack *s)
{if(s==NULL) return 1;
else return 0;
}
int Push(Sqstack *s,SElemType e)
{if(s->top-s->base>=s->stacksize)
{ s->base=(SElemType*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof
(SElemType));
if(!s->base)exit(0);
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return OK;
}
int pop(Sqstack *s,SElemType e)
{if (s->top==s->base) return ERROR;
e=*--s->top;
return OK;
}
BiTNode *CreateBinTree()/*建立二叉树,用#代表空指针,参见P127构造二叉树操作*/
{BiTNode *t;
char x;
scanf("%c",&x);
if(x=='#')t=NULL;
else{
t=(BiTNode*)malloc(sizeof(BiTNode));
t->data=x;
t->lchild=CreateBinTree();
t->rchild=CreateBinTree();}
return(t);}
char visit(char x)
{printf("%c",x);
return OK;
}
char PreOrderTraverse(BiTree T,char (*visit)(char x))
{if(T){
if(visit(T->data))
if(PreOrderTraverse(T->lchild,visit))
if(PreOrderTraverse(T->rchild,visit)) return OK;
return ERROR;
}else return OK;
}
char PostOrderTraverse(BiTree T,char (*visit)(char x))
{if(T){
if(PreOrderTraverse(T->lchild,visit))
if(PreOrderTraverse(T->rchild,visit))
if(visit(T->data)) return OK;
return ERROR;
}else return OK;
}
char InOrderTraverse(BiTree T,char (*visit)(char x))
{Sqstack *s;BiTNode *p;SElemType e;
s=InitStack();
p=T;
while(p||!StackEmpty(s)){
if(p){Push(s,p);p=p->lchild;}
else{pop(s,p);if(!visit(p->data))return ERROR;
p=p->rchild;
}
}
return OK;
}
changlr(BiTree T)
{BiTNode *p;
BiTNode *t;
Sqstack *s;
s=InitStack();
p=T;
while(p||!StackEmpty(s))
{if(p){Push(s,p);p=p->lchild;}
else
{pop(s,p);
t=p->lchild;
p->lchild=p->rchild;
p->rchild=t;
p=p->lchild;
}
}return OK;
}
void main()
{char visit(char x);
BiTNode *r;
printf("create bintree\n");
r=CreateBinTree();
printf("root order search\n");
PreOrderTraverse(r,visit);
printf("\nmiddle order search\n");
InOrderTraverse(r,visit);
printf("\nlast order search\n");
PostOrderTraverse(r,visit);
changlr(r);
printf("\nbeing changed root order search\n");
PreOrderTraverse(r,visit);
}