一直跑不出来结果,但是也看不出来问题……
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define Max 20
typedef struct BTNode
{
char data;
BTNode *l,*r;
}BTNode,*BTree;
//二叉树
void BuildTree(BTree &T)
{
char x=getchar();
if(x=='#') T=NULL;
else
{
T=(BTNode*)malloc(sizeof(BTNode));
T->data=x;
T->l=NULL;
T->r=NULL;
BuildTree(T->l);
BuildTree(T->r);
}
}
typedef struct Stack
{
struct BTNode *data[Max];
int top;
}Stack;
typedef struct LinkNode
{
BTree data;
LinkNode *next;
}LinkNode;
typedef struct LinkQueue
{
LinkNode *f,*r;
}LinkQueue;
//栈相关
void InitStack(Stack S)
{
S.top=-1;
}
bool isFull(Stack S)
{
if(S.top==Max-1)
return true;
}
bool isEmpty(Stack S)
{
if(S.top==-1)
return true;
}
bool pop(Stack &S,BTNode *&x)
{
if(isEmpty(S))
{
cout<<"栈空";
return false;
}
else
x=S.data[S.top--];
return true;
}
bool push(Stack &S,BTNode *x)
{
if(isFull(S))
{
cout<<"栈满";
return false;
}
else
S.data[++S.top]=x;
return true;
}
//队列相关
void InitQueue(LinkQueue &Q)
{
Q.f=Q.r=(LinkNode*)malloc(sizeof(LinkNode));
Q.f->next=NULL;
}
bool QisEmpty(LinkQueue Q)
{
if(Q.f=Q.r)
return true;
}
void EnQueue(LinkQueue &Q,BTNode *x)
{
LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=x;
s->next=NULL;
Q.r->next=s;
Q.r=s;
}
void DeQueue(LinkQueue &Q,BTNode *&x)
{
if(QisEmpty(Q))
{
cout<<"队列已空";
exit(0);
}
LinkNode *p=Q.f->next;
Q.f->next=p->next;
x=p->data;
cout<<"x的值为:"<<"";
free(p);
}
void LevelOrder(BTree T)
{
LinkQueue Q;
Stack S;
BTree p=NULL;
if(T!=NULL)
{
InitStack(S);
InitQueue(Q);
EnQueue(Q,T);
while(!QisEmpty(Q))
{
DeQueue(Q,p);
push(S,p);
if(p->l!=NULL)
EnQueue(Q,p->l);
if(p->r!=NULL)
EnQueue(Q,p->r);
}
while(!isEmpty(S))
{
pop(S,p);
cout<<"更新后的顺序为:"<<p->data<<"";
}
}
}
int main()
{
BTree T;
BuildTree(T);
LevelOrder(T);
}