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 ...
随机推荐
- java超大文件上传
上周遇到这样一个问题,客户上传高清视频(1G以上)的时候上传失败. 一开始以为是session过期或者文件大小受系统限制,导致的错误. 查看了系统的配置文件没有看到文件大小限制, web.xml中se ...
- 单调队列优化DP——习题收集
前言 感觉可以用单调队列优化dp的模型还是挺活的,开个随笔记录一些遇到的比较有代表性的模型,断续更新.主要做一个收集整理总结工作. 记录 0x01 POJ - 1821 Fence,比较适合入门的题, ...
- 【java设计模式】-02工厂模式
工厂模式简述 工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在工厂模式中,我们在创建对象时不会对客 ...
- html,css,js实现的一个钟表
效果如图: 实现代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- 新版iTunes connect上传iOS应用
http://www.brianjcoleman.com/tutorial-distribute-apps-using-new-itunes-connect/ Recently Apple updat ...
- ANDROID_ID
在设备首次启动时,系统会随机生成一个64位的数字,并把这个数字以16进制字符串的形式保存下来,这个16进制的字符串就是ANDROID_ID,当设备被wipe后该值会被重置.可以通过下面的方法获取: i ...
- HearthBuddy 突袭 rush
https://hearthstone.gamepedia.com/Rush Rush is an ability allowing a minion to attack other minions ...
- [MyBatis]完整MyBatis CRUD工程
下载地址:https://files.cnblogs.com/files/xiandedanteng/Person191005.rar pom.xml:这个文件主要是引入依赖 <project ...
- python异常值检验实战2_医美手术价格
python信用评分卡建模(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_ca ...
- 加载大图片的OOM异常
* 原因:系统分配给应用程序的堆内存 < 图片的大小* 解决方案:缩放图片显示* OOM:OutOfMemoryError * 图片的宽高 * 宽 2400 * 高 3200 * 手机屏幕的宽高 ...