采用先序输入二叉树各元素值建立二叉树(二叉链表存储结构),采用中序遍历非递归的算法输出各个元素
#include<stdio.h>#include<malloc.h>#include<string.h>#defineOK1#defineERROR0#defineTR...
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef char ElemType;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct SNode
{
ElemType data;
struct SNode *next;
}StackNode,*LinkStack;
Status InitBiTree(BiTree &T)
{
T=NULL;
return OK;
}
Status CreateBiTree(BiTree & T)
{
char ch;
scanf("%c",&ch);
if(ch=='#')T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!T)return ERROR;
else
{
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
return OK;
}
Status StackEmpty (LinkStack S )
{
if(S==NULL)return ERROR;
return OK;
}
Status InitStack(LinkStack &S)
{
S=NULL;
return OK;
}
Status Push(LinkStack &S,ElemType e)
{
LinkStack p;
p=(LinkStack)malloc(sizeof(StackNode));
if(!p)return ERROR;
p->data=e;
p->next=S;
S=p;
return OK;
}
Status Pop(LinkStack &S,ElemType &e)
{
LinkStack p;
if(!StackEmpty(S))return ERROR;
e=S->data;
p=S;
S=S->next;
free(p);
return OK;
}
void InOrderTraverse(BiTree T,LinkStack S)
{
ElemType e;
BiTree p;
InitStack(S);
p=T;
while(p!=NULL||!StackEmpty(S))
{
if(p!=NULL)
{
Push(S,p->data);
p=p->lchild;
}
else
{
Pop(S,e);
printf("%c",p->data);
p=p->rchild;;
}
}
}
int main()
{
BiTree T;
LinkStack S;
CreateBiTree(T);
InitStack(S);
InOrderTraverse(T,S);
printf("\n");
return 0;
}
这个代码哪里有错,为什么T->data存不了值 展开
#include<malloc.h>
#include<string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef char ElemType;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
typedef struct SNode
{
ElemType data;
struct SNode *next;
}StackNode,*LinkStack;
Status InitBiTree(BiTree &T)
{
T=NULL;
return OK;
}
Status CreateBiTree(BiTree & T)
{
char ch;
scanf("%c",&ch);
if(ch=='#')T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!T)return ERROR;
else
{
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
return OK;
}
Status StackEmpty (LinkStack S )
{
if(S==NULL)return ERROR;
return OK;
}
Status InitStack(LinkStack &S)
{
S=NULL;
return OK;
}
Status Push(LinkStack &S,ElemType e)
{
LinkStack p;
p=(LinkStack)malloc(sizeof(StackNode));
if(!p)return ERROR;
p->data=e;
p->next=S;
S=p;
return OK;
}
Status Pop(LinkStack &S,ElemType &e)
{
LinkStack p;
if(!StackEmpty(S))return ERROR;
e=S->data;
p=S;
S=S->next;
free(p);
return OK;
}
void InOrderTraverse(BiTree T,LinkStack S)
{
ElemType e;
BiTree p;
InitStack(S);
p=T;
while(p!=NULL||!StackEmpty(S))
{
if(p!=NULL)
{
Push(S,p->data);
p=p->lchild;
}
else
{
Pop(S,e);
printf("%c",p->data);
p=p->rchild;;
}
}
}
int main()
{
BiTree T;
LinkStack S;
CreateBiTree(T);
InitStack(S);
InOrderTraverse(T,S);
printf("\n");
return 0;
}
这个代码哪里有错,为什么T->data存不了值 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询