/* 二叉树的前中后序遍历 */
#include <stdio.h>
#include <stdlib.h>
typedef struct treeNode *BinTree;
typedef struct stackNode *Stack;
/* 二叉树的描述结构 */
struct treeNode
{
int data;
struct treeNode *left;
struct treeNode *right;
};
/* 栈的结构 */
struct stackNode
{
BinTree node;
struct stackNode *next;
};
/* 链表实现堆栈 */
Stack createStack(){
Stack stack = (Stack)malloc(sizeof(struct stackNode));
stack->next = NULL;
return stack;
}
/* 判断栈是否为空 */
int stackIsEmpty(Stack stackTop){
if (stackTop->next)
return 0;
return 1;
}
/* 压栈 */
void stackPush(Stack stackTop, BinTree myNode){
if (myNode)
{
Stack midNode = (Stack)malloc(sizeof(struct stackNode));
midNode->node = myNode;
midNode->next = stackTop->next;
stackTop->next = midNode;
}
}
/* 出栈 */
BinTree stackPop(Stack stackTop){
if (stackIsEmpty(stackTop))
{
printf("Stack is empty!\n");
return NULL;
}
BinTree mid = stackTop->next->node;
Stack willFree = stack
【C语言】用链表实现的堆栈来实现二叉树的前中后序遍历
最新推荐文章于 2023-03-15 20:23:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)