-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
156 lines (93 loc) · 3.11 KB
/
Copy pathStack.java
File metadata and controls
156 lines (93 loc) · 3.11 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
public class Stack {
private int Maxsize;
private int [] StackArray;
private int top; // represent the index position of the last item on the stack
public Stack (int size ) {
this.Maxsize = size ;
this.top = -1 ;
this.StackArray = new int [Maxsize];
}
public void push (int num){
top ++ ;
StackArray[top] = num;
}
public /*long*/void pop (){
// int Old_top = top;
top--;
//return StackArray[Old_top];// Old_top;
}
public int top(){
return StackArray [top];
}
public boolean IsEmpty() {
return (top ==-1);
}
public boolean IsFull(){
return (Maxsize -1 == top );
}
public String postFixEval(String postFix){
Stack MyStak1 = new Stack(postFix.length()/2+1);
for (int i = 0; i<postFix.length();i ++){
char ch = postFix.charAt(i);
if ((ch == '*')|| (ch =='+')|| (ch == '-')|| (ch =='^')||(ch == '/')){
if (isOperation (c)){
int b = MyStak1.top; MyStak1.pop();
int a = MyStak1.top; MyStak1.pop();
if (ch == '*') MyStak1.push(a*b);
else if (ch == '+') MyStak1.push(a+b);
else if (ch == '-') MyStak1.push(a-b);
else if (ch == '^') MyStak1.push(a^b);
}
else {
MyStak1.push(ch - '0');
}
}
return MyStak1.top(); // return stack.top();
}
public Static String infixToPostfix(String infix){
Stack MyStack2 = new Stack(infix.length()/2);
String postfix = "";
for (int i = 0 ; i< infix.length();i++){
char c = infix.charAt(i);
if ((c != '*')||
(c != '+')||
(c != '-')||
(c != '/')||
(c !='^' )||
(c !='(' )||
(c !=')'))
MyStack2.push(c);
else if (c =='('){
MyStack2.push(c);
if (c==')') {
while (!MyStack2.IsEmpty()){
char t = MyStack2.pop();
if (t != '('){
postfix = postfix + t;
}else {
break;
}
}
}
}else if (c == '+' || c == '-' || c =='*'|| c == '/' ){
if (MyStack2.IsEmpty()){
MyStack2.push(c);
}else {
while (!MyStack2.IsEmpty()){
char t = MyStack2.pop();
if (t == '('){
MyStack2.push(c);
break;
}else if (t == '+'|| t =='-'|| t == '*'||t =='/'){
if (getPriority(t) < getpriority(c)){
MyStack2.push(c);
}
}
}
}
}
}
}
return
}
}