我不理解下面这个中缀转后缀的StackEmpty是指什么(没参数)
#include<stdio.h>
#define ElemType char
#define MaxSize 50
typedef struct {
ElemType data[MaxSize];
int top;
}SqStack;
void initStack(SqStack& S)
{
S.top = -1; //初始化栈顶指针
}
bool StackEmpty(SqStack S)
{
if (S.top == -1) //栈空
return true;
else
return false; //栈不空
}
bool Push(SqStack& S, ElemType x)
{
if (S.top == MaxSize - 1) //栈满 不能执行入栈操作
return false;
S.top++; //指针先加1,再入栈
S.data[S.top] = x;
return true;
}
bool Pop(SqStack& S, ElemType& x)
{
if (S.top == -1) //栈空 不能执行出栈操作
return false;
x = S.data[S.top]; //先出栈 指针再减1
S.top--;
return true;
}
bool GetPop(SqStack S, ElemType& x)
{
if (S.top == -1) //栈空报错
return false;
x = S.data[S.top]; //用x存储栈顶元素
return true;
}
int main()
{
SqStack S;
initStack(S);
char NifixExpression[] = { '(','(','A','+','B',')','*','C',')','-','(','E','-','F',')','\0' };
char PostfixExpression[60]; //在字符数组最后添加'\0',作为结束标志
int index = 0;
char* p = NifixExpression;
while (*p!='\0')
{
if (*p == '(') //如果为左括号,直接入栈
{
Push(S, *p);
p++;
}
else if (*p == ')')//若为右括号,依次弹出栈中运算符,直到'(',并删除'('--用出栈不保存数据实现
{
while (S.data[S.top] != '(')
{
char temp;
Pop(S, temp);
if (temp == '+' || temp == '-' || temp == '*' || temp == '/')
{
PostfixExpression[index] = temp;
index++;
}
}
char temp;
Pop(S, temp); //把左括号也出栈
p++;
}
else if (*p >= 'A' && *p <= 'Z') //这里用大写字母代替表达式中的数字,为大写字母则直接进入后缀表达式
{
PostfixExpression[index] = *p;
index++;
p++;
}
else
{
if (*p == '+' || *p == '-')
{
if (S.data[S.top] == '('||StackEmpty)//如果遍历到'+''-',且栈为空或栈顶为'(',直接入栈
{
Push(S, *p);
p++;
}
else {
while (S.data[S.top] != '('&&!StackEmpty)//依次弹出较高优先级运算符,直到'('
{
char temp;
Pop(S, temp);
PostfixExpression[index] = temp;
index++;
}
}
}
else
{
Push(S, *p);
p++;
}
}
}
if (!StackEmpty(S)) //若栈不为空,依次弹出其中的运算符
{
while (S.top != -1)
{
char temp;
Pop(S,temp);
PostfixExpression[index] = temp;
index++;
}
}
PostfixExpression[index] = '\0'; //在字符数组后加'\0',作为结束标志
printf("中缀表达式为:%s",NifixExpression);
printf("\n");
printf("后缀表达式为:%s", PostfixExpression);
}