forked from the-moonLight0/Hactober-fest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfixToPostfix.java
47 lines (45 loc) · 1.73 KB
/
infixToPostfix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package stack;
//problem link : https://practice.geeksforgeeks.org/problems/infix-to-postfix-1587115620/1/?track=SPC-Stack&batchId=154
// solution link: https://www.youtube.com/watch?v=-6KHfvHyrJw&t=183s
import java.util.Stack;
public class InfixToPostfix {
public static void main(String[] args) {
String str = "a+b*(c^d-e)^(f+g*h)-i";
System.out.println(infixToPostfix(str));
}
public static String infixToPostfix(String str) {
StringBuilder postfix = new StringBuilder();
Stack<Character> stack = new Stack<>();
for(int i = 0; i<str.length();i++){
char ch = str.charAt(i);
if(isCharacter(ch))
postfix.append(ch);
else if(ch=='(')
stack.push(ch);
else if(ch==')') {
while (!stack.isEmpty() && stack.peek() != '(')
postfix.append(stack.pop());
if(stack.peek()=='(')stack.pop();
}
else{
while (!stack.isEmpty() && stack.peek()!='(' && precedence(stack.peek(),ch))
postfix.append(stack.pop());
stack.push(ch);
}
}
while (!stack.isEmpty())
postfix.append(stack.pop());
return postfix.toString();
}
public static boolean isCharacter(char ch){
if( ch == '(' || ch ==')' || ch =='^' || ch=='*'|| ch=='/' || ch=='+' || ch=='-')
return false;
return true;
}
public static boolean precedence(char x1 , char x2){
if(x1=='^')return true;
if((x1=='*' || x1=='/') && x2!='^')return true;
if((x1=='+' || x1=='-') && (x2!='^' && x2!='*' && x2!='/'))return true;
return false;
}
}