Infix to Prefix conversion using two stacks
Infix : An expression is called the Infix expression if the operator appears in between the operands in the expression. Simply of the form (operand1 operator operand2).
Example : (A+B) * (C-D)
Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2).
Example : *+AB-CD (Infix : (A+B) * (C-D) )
Given an Infix expression, convert it into a Prefix expression using two stacks.
Examples:
Input : A * B + C / D
Output : + * A B/ C D Input : (A - B/C) * (A/K-L)
Output : *-A/BC-/AKL
分析:
- Traverse the infix expression and check if given character is an operator or an operand.
- If it is an operand, then push it into operand stack.
- If it is an operator, then check if priority of current operator is greater than or less than or equal to the operator at top of the stack. If priority is greater, then push operator into operator stack. Otherwise pop two operands from operand stack, pop operator from operator stack and push string operator + operand1 + operand 2 into operand stack. Keep popping from both stacks and pushing result into operand stack until priority of current operator is less than or equal to operator at top of the operator stack.
- If current character is ‘(‘, then push it into operator stack.
- If current character is ‘)’, then check if top of operator stack is opening bracket or not. If not pop two operands from operand stack, pop operator from operator stack and push string operator + operand1 + operand 2 into operand stack. Keep popping from both stacks and pushing result into operand stack until top of operator stack is an opening bracket.
- The final prefix expression is present at top of operand stack.
class Solution {
boolean isOperator(char C) {
return C == '-' || C == '+' || C == '*' || C == '/' || C == '^';
}
int getPriority(char C) {
if (C == '-' || C == '+') {
return ;
} else if (C == '*' || C == '/') {
return ;
} else if (C == '^') {
return ;
} else {
return ;
}
}
String infixToPrefix(String infix) {
Stack<Character> operators = new Stack<>();
Stack<String> operands = new Stack<String>();
for (int i = ; i < infix.length(); i++) {
if (infix.charAt(i) == '(') {
operators.push(infix.charAt(i));
}
else if (infix.charAt(i) == ')') {
while (!operators.empty() && operators.peek() != '(') {
String op1 = operands.pop();
String op2 = operands.pop();
char op = operators.pop();
String tmp = op + op2 + op1;
operands.push(tmp);
}
} else if (!isOperator(infix.charAt(i))) {
operands.push(infix.charAt(i) + "");
}
else {
while (!operators.empty() && getPriority(infix.charAt(i)) <= getPriority(operators.peek())) {
String op1 = operands.pop();
String op2 = operands.pop();
char op = operators.pop();
operands.push(op + op2 + op1);
}
operators.push(infix.charAt(i));
}
}
while (!operators.empty()) {
String op1 = operands.pop();
String op2 = operands.pop();
char op = operators.pop();
operands.push(op + op2 + op1);
}
return operands.peek();
}
}
Infix to Prefix conversion using two stacks的更多相关文章
- Postfix to Prefix Conversion & Prefix to Postfix Conversion
Postfix to Prefix Conversion Postfix: An expression is called the postfix expression if the operator ...
- Infix to postfix conversion 中缀表达式转换为后缀表达式
Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...
- Swift声明参考
一条声明可以在你的程序里引入新的名字和构造.举例来说,你可以使用声明来引入函数和方法,变量和常量,或者来定义 新的命名好的枚举,结构,类和协议类型.你也可以使用一条声明来延长一个已经存在的命名好的类型 ...
- Swift5 语言参考(六) 声明
一个声明引入了一个新的名称或构建到你的程序.例如,您使用声明来引入函数和方法,引入变量和常量,以及定义枚举,结构,类和协议类型.您还可以使用声明来扩展现有命名类型的行为,并将符号导入到其他地方声明的程 ...
- a note of R software write Function
Functionals “To become significantly more reliable, code must become more transparent. In particular ...
- SpringBoot @ConfigurationProperties详解
文章目录 简介 添加依赖关系 一个简单的例子 属性嵌套 @ConfigurationProperties和@Bean 属性验证 属性转换 自定义Converter SpringBoot @Config ...
- React 17 要来了,非常特别的一版
写在前面 React 最近发布了v17.0.0-rc.0,距上一个大版本v16.0(发布于 2017/9/27)已经过去近 3 年了 与新特性云集的 React 16及先前的大版本相比,React 1 ...
- Prefix to Infix Conversion
Infix : An expression is called the Infix expression if the operator appears in between the operands ...
- C++ Knowledge series Conversion & Constructor & Destructor
Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...
随机推荐
- 检查多个远程 Linux 系统是否打开了指定端口
如果想检查 50 多台服务器是否打开了指定的端口,该怎么做,要检查所有服务器并不容易,如果你一个一个这样做,完全没有必要,因为这样你将会浪费大量的时间.为了解决这种情况,我使用 nc 命令编写了一个 ...
- 010_linuxC++之_运算符重载
(一)运算符重载:运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型. (二)实现类不同对象里中变量的相加 (三)程序 #include <iostream> ...
- vue中前进刷新、后退缓存用户浏览数据和浏览位置的实践
vue中前进刷新.后退缓存用户浏览数据和浏览位置的实践 2018年07月07日 11:58:40 大灰狼的小绵羊哥哥 阅读数:4492 vue中,我们所要实现的一个场景就是: 1.搜索页面==&g ...
- openstack导入镜像
本文以制作CentOS7.2镜像为例,详细介绍手动制作OpenStack镜像详细步骤,解释每一步这么做的原因.镜像上传到OpenStack glance,支持以下几个功能: 支持密码注入功能(nova ...
- 预处理、const、static与sizeof-使用const与#define的特点及区别
1:#define只是用来做文本替换的.例如: #define PI 3.1415926 float angle; angle=*PI/; 那么,程序进行编译的时候,编译器会首先将“#define P ...
- js中的bind方法的实现方法
js中目前我遇见的改变作用域的5中方法:call, apply, eval, with, bind. var obj = { color: 'green' } function demo () { c ...
- 关于Math.random()
关于 Math.random() ,以前经常搞混淆,这次写个笔记专门记录下: Math.random() : 返回的是 0~1 之间的一个随机小数0<=r<1,即[0,1); 注意:这里 ...
- echart itemStyle属性设置
itemStyle // itemStyle 设置饼状图扇形区域样式 itemStyle: { // emphasis:英文意思是 强调;着重; ...
- Flume-安装与 NetCat UDP Source 监控端口
Flume 文档:https://flume.apache.org/FlumeUserGuide.html Flume 下载:https://archive.apache.org/dist/flume ...
- CNN中感受野大小的计算
1 感受野的概念 从直观上讲,感受野就是视觉感受区域的大小.在卷积神经网络中,感受野的定义是 卷积神经网络每一层输出的特征图(feature map)上的像素点在原始图像上映射的区域大小. 2 感受野 ...