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 ...
随机推荐
- eq(index|-index)
eq(index|-index) 概述 获取当前链式操作中第N个jQuery对象,返回jQuery对象,当参数大于等于0时为正向选取,比如0代表第一个,1代表第二个.当参数为负数时为反向选取,比如-1 ...
- PHP mysqli_get_charset() 函数
mysqli_get_charset() 函数返回字符集对象. <?php $con=mysqli_connect("localhost","my_user&quo ...
- 《30天自制操作系统》学习笔记--Mac下工具的使用
现在来介绍官网上下的工具怎么用首先是官网地址,书上有个注释上有:hrb.osask.jp 翻译成中文大概是这个样子滴. 上面有两个文件可以下载,一个是工具,一个是工具的源代码,很好的学习资料 下面把工 ...
- 0078 Java与MySQL时间戳传递/存储/协调问题--userLegacyDatetimeCode--userTimezone--serverTimezone
00. 基本问题 0.0 版本: 驱动5.1.47和8.0.17 0.1 MySQL驱动5.1有userLegacyDatetimeCode和userTimezone两个参数, 8.0没有 0.2 J ...
- Redis 延迟指标监控
Redis 延迟监控框架 Redis 2.8.13 引入了Latency Monitoring的一个新功能,可以帮助我们检查和排查引起延迟的原因. Latecny Monitoring 由如下组成: ...
- JVM模型及内存溢出
一.JVM截图及概念 图1:JVM虚拟机运行时数据区域概念模型 1.程序计数器:内存空间中的一块小区域,作为当前线程所执行的字节码的行号指示器,注:如果是native方法,计数器为空 2.虚拟机栈:线 ...
- UTC ISO 8601
如果时间在零时区,并恰好与协调世界时相同,那么(不加空格地)在时间最后加一个大写字母Z.Z是相对协调世界时时间0偏移的代号.如下午2点30分5秒表示为14:30:05Z或143005Z:只表示小时和分 ...
- Android解决AVD Hardware Buttons 和DPAD无法使用问题
如图所示按键用鼠标点击时无法响应. 解决方案: 以我创建的AVD名为Tablet为例 1.找到用户目录(我的用户目录yummy),然后进入如下目录 mac: ~/yummy/.android/avd/ ...
- leetcode94 不同的二叉搜索树
solution 1:**动态规划 class Solution { public: int numTrees(int n) { vector<int> g={1,1,2}; for(in ...
- 04 MySQL之函数
01-数学函数 数学函数主要用来处理数值数据. # 1.绝对值函数 ABS(x) 和 返回圆周率的函数 PI() ABS(x) 返回x的绝对值. 例: mysql> select ABS(2), ...