generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack5PrefixToPostfix.java
66 lines (57 loc) · 1.51 KB
/
stack5PrefixToPostfix.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
63
64
65
66
import java.util.*;
class Stack {
private int size;
private String[] arr;
private int top;
public Stack(int size) {
this.size = size;
top = -1;
arr = new String[size];
}
public void push(String s) {
arr[++top] = s;
}
public String pop() {
return arr[top--];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == size - 1);
}
}
public class stack5PrefixToPostfix{
public static boolean isOperator(char s) {
if (s == '+' || s == '-' || s == '*' || s == '/') {
return true;
} else {
return false;
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String prefix,temp1,temp2,temp;
String rev = "";
char current;
System.out.println("Enter the string:");
prefix = sc.nextLine();
Stack s = new Stack(prefix.length());
for (int i = prefix.length() - 1; i >= 0; i--) {
rev = rev + (prefix.charAt(i));
}
for(int i = 0;i< rev.length();i++){
current = rev.charAt(i);
if(Character.isLetterOrDigit(current) == true){
s.push("" + current);
}
else{
temp1 = s.pop();
temp2 = s.pop();
temp = temp1 + temp2 + current;
s.push(temp);
}
}
System.out.println(s.pop());
}
}