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
分析:
  1. Traverse the infix expression and check if given character is an operator or an operand.
  2. If it is an operand, then push it into operand stack.
  3. 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.
  4. If current character is ‘(‘, then push it into operator stack.
  5. 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.
  6. 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的更多相关文章

  1. Postfix to Prefix Conversion & Prefix to Postfix Conversion

    Postfix to Prefix Conversion Postfix: An expression is called the postfix expression if the operator ...

  2. Infix to postfix conversion 中缀表达式转换为后缀表达式

    Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...

  3. Swift声明参考

    一条声明可以在你的程序里引入新的名字和构造.举例来说,你可以使用声明来引入函数和方法,变量和常量,或者来定义 新的命名好的枚举,结构,类和协议类型.你也可以使用一条声明来延长一个已经存在的命名好的类型 ...

  4. Swift5 语言参考(六) 声明

    一个声明引入了一个新的名称或构建到你的程序.例如,您使用声明来引入函数和方法,引入变量和常量,以及定义枚举,结构,类和协议类型.您还可以使用声明来扩展现有命名类型的行为,并将符号导入到其他地方声明的程 ...

  5. a note of R software write Function

    Functionals “To become significantly more reliable, code must become more transparent. In particular ...

  6. SpringBoot @ConfigurationProperties详解

    文章目录 简介 添加依赖关系 一个简单的例子 属性嵌套 @ConfigurationProperties和@Bean 属性验证 属性转换 自定义Converter SpringBoot @Config ...

  7. React 17 要来了,非常特别的一版

    写在前面 React 最近发布了v17.0.0-rc.0,距上一个大版本v16.0(发布于 2017/9/27)已经过去近 3 年了 与新特性云集的 React 16及先前的大版本相比,React 1 ...

  8. Prefix to Infix Conversion

    Infix : An expression is called the Infix expression if the operator appears in between the operands ...

  9. C++ Knowledge series Conversion & Constructor & Destructor

    Everything has its lifecycle, from being created to disappearing. Pass by reference instead of pass ...

随机推荐

  1. removeClass([class|fn])

    removeClass([class|fn]) 概述 从所有匹配的元素中删除全部或者指定的类.直线电机生产厂家   参数 classStringV1.0 一个或多个要删除的CSS类名,请用空格分开 f ...

  2. ubuntu配置pip3以及scrapy

    .安装支持pip3 sudo apt-get install python3-pip .安装scrapy 首先需要安装scrapy依赖项,否则scrapy安装失败,执行如下命令: sudo apt-g ...

  3. 数据结构实验之链表三:链表的逆置(SDUT 2118)

    题目链接 #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; ...

  4. Python基础之enumerate枚举

    枚举,对于一个可迭代的(iterable)/可遍历的对象(如列表,字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值. 1. 第一种类型 lst = ["a&quo ...

  5. 1.RabbitMq - Work 模式

    RabbitMq - Work 模式 一.什么是Work模式 如果有几个消息都需要处理,且每个消息的处理时间很长,仅有一个消费者,那么当它在处理一个消息的时候,其他消息就只有等待. 等待有时候是好的, ...

  6. 02 Anaconda的介绍,安装记以及使用

    目录 〇.序 Python是一种面向对象的解释型计算机程序设计语言,其使用,具有跨平台的特点,可以在Linux.macOS以及Windows系统中搭建环境并使用,其编写的代码在不同平台上运行时,几乎不 ...

  7. openwrt系统源码地址

    https://dev.openwrt.org/wiki/GetSource http://www.openwrtdl.com/wordpress/openwrt-full-tutorial

  8. 微PE:装机不求人,教你制作最纯净的PE安装系统

    https://www.jianshu.com/p/50fd699ea916 超好用的PE工具,免费.纯净.无广告,装系统必备! https://www.jianshu.com/p/fecf090b2 ...

  9. sed与awk

    sed 格式 sed 选项 控制命令 文件或标准输入 sed 流程: (循环打印) sed是将文件里的每一行读入模式空间进行操作, sed选项 -r 支持正则表达 -n 取消默认打印 清空当前模式空间 ...

  10. HttpURLConnection 多线程下载

    影响下载的速度 * 宽带的带宽 * 服务器的限制 * 服务器的资源固定,开启的线程越多抢占的资源就越多 import java.io.InputStream; import java.io.Rando ...