diff --git a/Java/InfixToPostfix.java b/Java/InfixToPostfix.java new file mode 100644 index 0000000..1bef270 --- /dev/null +++ b/Java/InfixToPostfix.java @@ -0,0 +1,92 @@ +import java.util.Stack; +import java.util.Scanner; + +public class InfixToPostfix +{ + // A utility function to return precedence of a given operator + // Higher returned value means higher precedence + static int Prec(char ch) + { + switch (ch) + { + case '+': + case '-': + return 1; + + case '*': + case '/': + return 2; + + case '^': + return 3; + + default: + return; + } + return -1; + } + + // The main method that converts given infix expression + // to postfix expression. + static String infixToPostfix(String exp) + { + // initializing empty String for result + String result = new String(""); + + // initializing empty stack + Stack stack = new Stack<>(); + + for (int i = 0; i