Write a program to convert Infix to Prefix using stack
Example 1:
Input: infix="A+B*C+(E/F)*G^H"
Output: prefix= "+A+*BC*/EF^GH"
Approach :
Java
import java.util.HashMap;import java.util.Stack;public class InfixToPrefix {static HashMap<Character, Integer> precedence =new HashMap<Character, Integer>();public static void main(String[] args) {// infixString infix = "A+B*C+(E/F)*G^H";// precedence initializationoperatorPrecedence();// infix to prefixString prefix = infixToPrefix(infix);System.out.println("Prefix is " + prefix);}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;}int main(){// infixstring infix = "A+B*C+(E/F)*G^H";// precedence initializationoperatorPrecedence();// infix to prefixstring prefix = infixToPrefix(infix);cout<<"Prefix is ";cout<<prefix<<"\n";return 0;}
No comments:
Post a Comment