forked from itsrits29/JAVA2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringques.java
62 lines (49 loc) · 1.38 KB
/
stringques.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.*;
class GFG {
static boolean isOperator(char x)
{
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
}
return false;
}
// Convert prefix to Postfix expression
static String preToPost(String pre_exp)
{
Stack<String> s = new Stack<String>();
// length of expression
int length = pre_exp.length();
for (int i = length - 1; i >= 0; i--)
{
// check if symbol is operator
if (isOperator(pre_exp.charAt(i)))
{
// pop two operands from stack
String op1 = s.peek();
s.pop();
String op2 = s.peek();
s.pop();
// concat the operands and operator
String temp = op1 + op2 + pre_exp.charAt(i);
// Push String temp back to stack
s.push(temp);
}
else {
// push the operand to the stack
s.push(pre_exp.charAt(i) + "");
}
}
return s.peek();
}
// Driver Code
public static void main(String args[])
{
String pre_exp = "*-A/BC-/AKL";
System.out.println("Postfix : "
+ preToPost(pre_exp));
}
}