RT
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Node
{
char data;
struct Node *Lchild;
struct Node *Rchild;
}BitNode,*Bitree;
typedef struct Seq
{
char elem[100];
int top;
}SeqStack;
Bitree CreateBitree()
{
char ch;
Bitree bt;
ch=getchar();
if(ch=='#') return(NULL);
bt=(Bitree)malloc(sizeof(BitNode));
bt->data=ch;
bt->Lchild=CreateBitree();
bt->Rchild=CreateBitree();
return bt;
}
SeqStack *InitStack()
{
SeqStack *s;
s=(SeqStack*)malloc(sizeof(SeqStack));
s->top=-1;
return s;
}
int IsEmpty(SeqStack *S)
{
if(S->top==-1)
{
return 1;
}
else
{
return 0;
}
}
int Push(SeqStack *S,char x)
{
S->top++;
S->elem[S->top]=x;
}
int Pop(SeqStack *S,char *x)
{
if(S->top==-1)
{
return 0;
}
else
{
*x=S->elem[S->top];
S->top--;
return 1;
}
}
void PreOrder(Bitree T)
{
SeqStack *S;
S=InitStack();
Bitree p=T;
while (p||!IsEmpty(S))
{
if (p!=NULL)
{
printf("%c",p->data);
Push(S,p->data);
p=p->Lchild;
}
else
{
Pop(S,&p->data);
p=p->Rchild;
}
}
}
main()
{
Bitree T;
T=CreateBitree();
PreOrder(T);
}
输入ABC##DE#G##F###
输出只有ABC,然后弹错,求大神解答
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct Node
{
char data;
struct Node *Lchild;
struct Node *Rchild;
}BitNode,*Bitree;
typedef struct Seq
{
char elem[100];
int top;
}SeqStack;
Bitree CreateBitree()
{
char ch;
Bitree bt;
ch=getchar();
if(ch=='#') return(NULL);
bt=(Bitree)malloc(sizeof(BitNode));
bt->data=ch;
bt->Lchild=CreateBitree();
bt->Rchild=CreateBitree();
return bt;
}
SeqStack *InitStack()
{
SeqStack *s;
s=(SeqStack*)malloc(sizeof(SeqStack));
s->top=-1;
return s;
}
int IsEmpty(SeqStack *S)
{
if(S->top==-1)
{
return 1;
}
else
{
return 0;
}
}
int Push(SeqStack *S,char x)
{
S->top++;
S->elem[S->top]=x;
}
int Pop(SeqStack *S,char *x)
{
if(S->top==-1)
{
return 0;
}
else
{
*x=S->elem[S->top];
S->top--;
return 1;
}
}
void PreOrder(Bitree T)
{
SeqStack *S;
S=InitStack();
Bitree p=T;
while (p||!IsEmpty(S))
{
if (p!=NULL)
{
printf("%c",p->data);
Push(S,p->data);
p=p->Lchild;
}
else
{
Pop(S,&p->data);
p=p->Rchild;
}
}
}
main()
{
Bitree T;
T=CreateBitree();
PreOrder(T);
}
输入ABC##DE#G##F###
输出只有ABC,然后弹错,求大神解答