-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrefixToPostfix.java
More file actions
36 lines (33 loc) · 971 Bytes
/
PrefixToPostfix.java
File metadata and controls
36 lines (33 loc) · 971 Bytes
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
import java.util.Stack;
public class PrefixToPostfix {
private static boolean isOperator(char x) {
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
}
return false;
}
public static String convert(String prefix) {
Stack<String> stack = new Stack<>();
int length = prefix.length();
for (int i = length - 1; i >= 0; i--) {
char c = prefix.charAt(i);
if (isOperator(c)) {
String op1 = stack.pop();
String op2 = stack.pop();
String temp = op1 + op2 + c;
stack.push(temp);
} else {
stack.push(c + "");
}
}
return stack.pop();
}
public static void main(String[] args) {
String prefixExp = "*-A/BC-/AKL";
System.out.println("Postfix : " + convert(prefixExp));
}
}