之前按照数据结构的课本(李忠月版)实现了利用两个栈来进行中缀表达式的计算,下面这段代码可以实现利用二叉树来进行中缀表达式的计算,计算过程中不适用栈,仅利用二叉树的后序遍历思想。
#include<stdio.h>
#include<stdlib.h>
#define INIT_SIZE 10
#define INCREMENTSIZE 10
typedef struct List
{
char *elem;
int length;
int size;
}List;
typedef struct SNode
{
char data;
struct SNode *next;
}SNode,*LinkStack;
typedef struct TNode
{
char data;
struct TNode *lchild;
struct TNode *rchild;
}TNode,*Tree;
int InitList(List*L)
{
(*L).elem = (char *)malloc(INIT_SIZE*sizeof(char));
if(!(*L).elem)
{
return -1;
}
(*L).length = 0;
(*L).size = INIT_SIZE;
return 0;
}
int InitStack(LinkStack *S)
{
(*S) = (LinkStack)malloc(sizeof(SNode));
if(!S)
{
return -1;
}
(*S)->next = NULL;
return 0;
}
int InitTree(Tree *T)
{
(*T) = (Tree)malloc(sizeof(TNode));
if(!T)
{
return -1;
}
(*T)->