Write a program to Postfix to Prefix conversion using stack
Example: 1
Input: postfix = "ABC*+EF/GH^*+"
Output: prefix="+A+*BC/E*F^GH"
Approach:
Java
import java.util.HashMap;import java.util.Stack;public class PostfixPrefix {static HashMap<Character, Integer> precedence =new HashMap<Character, Integer>();public static void main(String[] args) {// postFIxString postFIx = "ABC*+EF/GH^*+";// precedence initializationoperatorPrecedence();// postfix to infixString infix = postfixToInfix(postFIx);System.out.println(infix);// infix to prefixString prefix = infixToPrefix(infix);System.out.println("Prefix is " + prefix);}private static String postfixToInfix(String postFIx) {String infix = "";// stack for hold operatorStack<String> stack = new Stack<>();// Iterate till given infix lengthfor (int i = 0; i < postFIx.length(); i++) {// hold char from postFIxchar c = postFIx.charAt(i);// If letter then add into stackif (Character.isLetter(c)) {stack.add(String.valueOf(c));} else {// rule = second+operator+firstString a = stack.pop();String b = stack.pop();stack.add(b + "" + c + "" + a);}}// iterate till stack is emptywhile (!stack.isEmpty()) {infix += stack.pop();}return infix;}private static String infixToPrefix(String infix) {String preFix = "";// stack for hold operatorStack<Character> stack = new Stack<>();// Iterate till given infix lengthinfix = new StringBuffer(infix).reverse().toString();for (int i = 0; i < infix.length(); i++) {// hold char from infixchar c = infix.charAt(i);// If letter then add into preFixif (Character.isLetter(c)) {preFix += infix.charAt(i);} else if (c == ')') {stack.add(c);} else if (c == '^') {stack.add(c);} else if (c == '(') { // if closing ) then// iterate till ( and add into preFixwhile (!stack.isEmpty() && stack.peek() != ')') {preFix += stack.pop();}// if last char is ')' into stack then popif (!stack.isEmpty() && stack.peek() == ')')stack.pop();} else {// if precedence of c is greater//then top stack then addif (stack.isEmpty() || precedence.get(c) >precedence.getOrDefault(stack.peek(), 0)) {stack.add(c);} else {// iterate till precedence is less then equal and addwhile (!stack.isEmpty() && precedence.get(c)<= precedence.get(stack.peek())) {preFix += stack.pop();}// add coming charstack.add(c);}}}// iterate till stack is emptywhile (!stack.isEmpty()) {preFix += stack.pop();}preFix = new StringBuffer(preFix).reverse().toString();return preFix;}private static HashMap<Character, Integer> operatorPrecedence() {precedence.put('^', 10);precedence.put('*', 9);precedence.put('/', 9);precedence.put('-', 8);precedence.put('+', 8);return precedence;}}
C++
#include <bits/stdc++.h>using namespace std;map<char,int> precedence;map<char,int> operatorPrecedence(){precedence['^']=10;precedence['*']=9;precedence['/']=9;precedence['-']=8;precedence['+']=8;return precedence;}string infixToPrefix(string infix){string prefix = "";// stack for hold operatorstack<char> stack;//revers the infix stringreverse(infix.begin(),infix.end());// Iterate till given infix lengthfor (int i = 0; i < infix.size(); i++){// hold char from infixchar c = infix[i];// If letter then add into prefixif (isalpha(c)) {prefix += c;}else if (c == ')'){stack.push(c);}else if (c == '^'){stack.push(c);}else if (c == '('){ // if closing ( then// iterate till ) and add into prefixwhile (!stack.empty() && stack.top() != ')') {{prefix += stack.top();stack.pop();}}// if last char is ')' into stack then popif (!stack.empty() && stack.top() == ')')stack.pop();} else {// if precedence of c is greater then//top stack then addif (stack.empty() || precedence[c] >precedence[stack.top()]) {stack.push(c);} else {// iterate till precedence is less then equal and addwhile (!stack.empty() && precedence[c]<= precedence[stack.top()]) {{prefix += stack.top();stack.pop();}}// add coming charstack.push(c);}}}// iterate till stack is emptywhile (!stack.empty()) {{prefix += stack.top();stack.pop();}}reverse(prefix.begin(),prefix.end());return prefix;}//unction to convert from//postfix to infixstring postfixToInfix(string postFIx){string infix = "";//stack for hold operatorstack<string> stack;// Iterate till given postfix lengthfor (int i = 0; i < postFIx.size(); i++){//hold char from postFIxchar c = postFIx[i];// If letter then add into stackif (isalpha(c)){//convert from character//to stringstring strc(1,c);stack.push(strc);}else{// rule = second+operator+firststring a = stack.top();stack.pop();string b = stack.top();stack.pop();//convert from character//to stringstring strc(1,c);stack.push(b + "" + strc + "" + a);}}//iterate till stack is emptywhile (!stack.empty()) {{infix += stack.top();stack.pop();}}return infix;}int main(){// postfixstring postFIx = "ABC*+EF/GH^*+";//initialize the operator//precedenceoperatorPrecedence();//postfix to infixstring infix = postfixToInfix(postFIx);//infix to prefixstring prefix=infixToPrefix(infix);cout<<"Prefix is ";cout<<prefix<<"\n";return 0;}
No comments:
Post a Comment