-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizeableArrayStack.java
More file actions
136 lines (115 loc) · 3.81 KB
/
ResizeableArrayStack.java
File metadata and controls
136 lines (115 loc) · 3.81 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
import java.util.Arrays;
import java.util.EmptyStackException;
/**
* An implementation of the ADT stack using a resizeable array.
*/
public final class ResizeableArrayStack<T> implements StackInterface<T>
{
private T[] stack; // Array of stack entries
private int topIndex; // Index of top entry
private boolean integrityOK = false;
private static final int DEFAULT_CAPACITY = 50;
private static final int MAX_CAPACITY = 10000;
public ResizeableArrayStack()
{
this(DEFAULT_CAPACITY);
} // end default constructor
public ResizeableArrayStack(int initialCapacity)
{
integrityOK = false;
checkCapacity(initialCapacity);
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempStack = (T[])new Object[initialCapacity];
stack = tempStack;
topIndex = -1;
integrityOK = true;
} // end constructor
/** Checks that the capacity is not greater than the max capacity allowed.
* @param capacity The given capacity to be checked for validity.
*/
private void checkCapacity(int capacity) {
if (capacity > MAX_CAPACITY) {
throw new IllegalStateException("Attempt to create a bag whose " +
"capacity exceeds allowed " +
"maximum of " + MAX_CAPACITY);
}
} // end checkCapacity
/**
* Checks if stack is at size limit, if it is create a new stack of double size.
*/
private void ensureCapacity() {
if (topIndex >= stack.length - 1) {
int newLength = 2 * stack.length;
checkCapacity(newLength);
stack = Arrays.copyOf(stack, newLength);
}
} // end ensureCapacity
/**
* Check if the integrity of the stack is maintained.
*/
private void checkIntegrity() {
if (!integrityOK) {
throw new SecurityException("ArrayStack object is corrupt");
}
} // end checkIntegrity
/** Adds newEntry to the top of the stack.
* @param newEntry The entry being added to the top of the stack.
*/
@Override
public void push(T newEntry) {
checkIntegrity();
ensureCapacity();
stack[topIndex + 1] = newEntry;
topIndex++;
} // end push
/** Remove the top of the stack and return the data of that element.
* @return T The data of the element at the top of the stack.
*/
@Override
public T pop() {
checkIntegrity();
if (isEmpty()) {
throw new EmptyStackException();
} else {
T top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
return top;
}
} // end pop
/** Looks at the data of element at the top of the stack, if there is one
* returns the data, if not it throws an exception.
* @return T The data of the element at the top of the stack.
*/
@Override
public T peek() {
checkIntegrity();
if (isEmpty()) {
throw new EmptyStackException();
} else {
return stack[topIndex];
}
} // end peek
/** Checks if the stack is empty.
* @return boolean True if the stack is empty, false if not.
*/
@Override
public boolean isEmpty() {
return topIndex < 0;
} // end isEmpty
/**
* Clears the stack by removing references to all the objects.
*/
@Override
public void clear() {
checkIntegrity();
// Remove refferences to the objects in the stack
// but do not deallocate the array
while (topIndex > -1) {
stack[topIndex] = null;
topIndex--;
}
// Assertion: topIndex is -1
} // end clear
}