栈的基本操作算法的实现
基本数据结构
typedef struct BiNode
{
char data;
struct BiNode *lchild,*rchild;
}BiNode,*BiTree;
{
char data;
struct BiNode *lchild,*rchild;
}BiNode,*BiTree;
//
栈的数据结构
//
栈元素类型
typedef
struct
{
BiNode *stack[maxn];
//
存储节点数组
int
top;
//
栈顶指针
}BStack,*Stack;
基本操作
//
栈初始化
Stack InitStack()
{
Stack S;
S=(BStack*)malloc(
sizeof
(BStack));
if
(S)
S->top=-1;
else
printf(
"Init stack error!\n"
);
return
S;
}
int
StackEmpty(Stack S)
//
判断栈是否为空
{
if
(S->top==-1)
//
若为空,则返回
return
1;
else
return
0;
}
int
StackFull(Stack S)
//
判断栈是否满
{
if
(S->top==maxn-1)
//
栈满则返回
return
1;
else
return
0;
}
void
Push(Stack &S,BiNode *e)
//
进栈操作,因为栈要发生变化,所以栈要用引用型
{
if
(!(StackFull(S)))
//
进栈之前要判断栈是否满,若满,则不能进栈
{
S->top++;
//
先移动栈顶指针,再压入元素
S->stack[S->top]=e;
}
else
return
;
}
BiNode* Pop(Stack &S)
//
出栈操作,即弹出栈顶元素,用一个节点保存弹出的栈顶元素,作为返回值
{
BiNode *e;
if
(!(StackEmpty(S)))
//
出栈之前,要判断栈是否为空,若为空,则无效操作
{
e=S->stack[S->top];
//
先挪出元素,再移动栈顶指针
S->top--;
return
e;
}
else
return
NULL;
}
BiNode* GetsTop(Stack S)
{
if
(StackEmpty(S))
return
NULL;
else
return
S->stack[S->top];
}
void
Free(Stack &S)
{
free(S);
}
以上栈的实现可以用于二叉树的先序遍历和中序遍历非递归算法的实现。
因为二叉树的后序非递归遍历算法的实现较前面两种相对复杂,故给出了另外一种新的栈的实现。
其实只是存储元素稍微有些不同。其他的基本操作实现差不多一样。
基本代码如下:
后序非递归遍历实现所需要的栈的结构
//
栈的数据结构
//
栈元素类型
typedef
struct
{
BiNode *q;
//
存放结点地址
int
tag;
//
存放当前状态位
}SNode;
typedef
struct
{
SNode stack[maxn];
//
存储节点数组
int
top;
//
栈顶指针
}BStacks,*Stacks;
//
栈初始化
Stacks InitStacks()
{
Stacks S;
S=(BStacks*)malloc(
sizeof
(BStacks));
if
(S)
S->top=-1;
else
printf(
"Init stack error!\n"
);
return
S;
}
int
StacksEmpty(Stacks S)
//
判断栈是否为空
{
if
(S->top==-1)
//
若为空,则返回
return
1;
else
return
0;
}
int
StacksFull(Stacks S)
//
判断栈是否满
{
if
(S->top==maxn-1)
//
栈满则返回
return
1;
else
return
0;
}
void
Pushs(Stacks &S,SNode *e)
//
进栈操作,因为栈要发生变化,所以栈要用引用型
{
if
(!(StacksFull(S)))
//
进栈之前要判断栈是否满,若满,则不能进栈
{
S->top++;
//
先移动栈顶指针,再压入元素
S->stack[S->top]=*e;
}
else
return
;
}
SNode* Pops(Stacks &S)
//
出栈操作,即弹出栈顶元素,用一个节点保存弹出的栈顶元素,作为返回值
{
SNode *e;
if
(!(StacksEmpty(S)))
//
出栈之前,要判断栈是否为空,若为空,则无效操作
{
e=&(S->stack[S->top]);
//
先挪出元素,再移动栈顶指针
S->top--;
return
e;
}
else
return
NULL;
}
SNode* GetsTop(Stacks S)
{
if
(StacksEmpty(S))
return
NULL;
else
return
&(S->stack[S->top]);
}
void
Free(Stacks &S)
{
free(S);
}
下面给出二叉树层次遍历所需要的数据结构:队列。
二叉树层次遍历用到的数据结构:队列。
typedef
struct
Node
{
BiNode *data;
struct
Node *next;
}LQueNode;
typedef
struct
{
LQueNode *front,*rear;
}LinkQueue;
void
InitQueue(LinkQueue *&Q)
{
Q=(LinkQueue *)malloc(
sizeof
(LinkQueue));
Q->front=Q->rear=NULL;
}
int
QueueEmpty(LinkQueue *Q)
{
if
(Q->front==NULL||Q->rear==NULL)
return
1;
else
return
0;
}
void
EnQueue(LinkQueue *&Q,BiNode *e)
{
LQueNode *p;
p=(LQueNode *)malloc(
sizeof
(LQueNode));
p->data=e;
p->next=NULL;
if
(Q->rear==NULL)
Q->front=Q->rear=p;
else
{
Q->rear->next=p;
Q->rear=p;
}
}
BiNode* DeQueue(LinkQueue *&Q)
{
BiNode *e;
LQueNode *p;
if
(Q->rear==NULL)
//
队空不能出列
return
NULL;
else
p=Q->front;
if
(Q->front==Q->rear)
//
队列中只有一个元素时的出队操作需特殊处理
Q->front=Q->rear=NULL;
else
Q->front=Q->front->next;
e=p->data;
free(p);
return
e;
}
我会在下一篇文章里,给出二叉树的四种遍历算法以及相应的非递归算法。