-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResizeableArrayStack.java
More file actions
134 lines (121 loc) · 3.45 KB
/
ResizeableArrayStack.java
File metadata and controls
134 lines (121 loc) · 3.45 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
import java.util.EmptyStackException;
import java.util.Arrays;
public class ResizeableArrayStack<T> implements StackInterface<T>
{
private T[] stack;
private int topIndex;
private boolean integrityOK = false;
private static final int DEFAULT_CAPACITY = 50;
private static final int MAX_CAPACITY = 10000;
public ResizeableArrayStack()
{
this(DEFAULT_CAPACITY);
}
public ResizeableArrayStack(int inititalCapacity)
{
checkCapacity(inititalCapacity);
@SuppressWarnings("unchecked")
T[] tempStack =(T[])new Object[inititalCapacity];
stack = tempStack;
topIndex = -1;
integrityOK = true;
}
public void push(T newEntry)
{
checkIntegrity();
ensureCapacity();
stack[topIndex+1] = newEntry;
topIndex++;
}
public T pop()
{
checkIntegrity();
if (isEmpty())
{
throw new EmptyStackException();
}
else
{
T top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
return top;
}
}
public T peek()
{
checkIntegrity();
if (isEmpty())
throw new EmptyStackException();
else
return stack[topIndex];
}
public boolean isEmpty()
{
return topIndex < 0;
}
public void clear()
{
checkIntegrity();
while (topIndex > -1)
{
stack[topIndex] = null;
topIndex--;
}
}
public int evaluatePostfix(String entry){
checkIntegrity();
ResizeableArrayStack<Integer> stack1 = new ResizeableArrayStack<>();
for(int i=0, temp; i<entry.length(); i++){
switch(entry.charAt(i)){
case 'a': case 'b': case 'c': case 'd': case 'e':
stack1.push(Character.getNumericValue(entry.charAt(i))-8);
break;
case '+':
stack1.push(stack1.pop() + stack1.pop());
break;
case '-':
temp = stack1.pop();
stack1.push(stack1.pop() - temp);
break;
case '*':
stack1.push(stack1.pop() * stack1.pop());
break;
case '/':
temp = stack1.pop();
stack1.push(stack1.pop() / temp);
break;
case '^':
temp = stack1.pop();
int base = stack1.pop();
for(int j=1; j<temp; j++){
base *= base;
}
stack1.push(base);
break;
default:
break;
}
}
return stack1.peek();
}
private void checkCapacity(int capacity){
if(capacity > MAX_CAPACITY){
throw new IllegalStateException("Attempt to create a list whos capacity is above maximum");
}
}
private void ensureCapacity(){
if(topIndex >= stack.length -1){
int newLength = 2 * stack.length;
checkCapacity(newLength);
stack = Arrays.copyOf(stack, newLength);
}
}
private void checkIntegrity()
{
if(!integrityOK)
{
throw new SecurityException("Array Bag object is corrupt");
}
}
}