-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathStack.java
74 lines (59 loc) · 1.97 KB
/
Stack.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
67
68
69
70
71
72
73
74
import java.util.*;
public class App {
/** A stack of integers, with a maximum size. */
static final class IntStack {
private int top;
private final int[] values;
/** Creates an empty stack with the given max size; it is forbidden to push more items than that. */
public IntStack(int maxSize) {
// BUGFIX: argument validation
if (maxSize < 0) {
throw new IllegalArgumentException("Max size must be >=0");
}
top = -1;
values = new int[maxSize];
assertInvariant();
}
/** Returns and pops the top of the stack, or returns null if the stack is empty. */
public Integer pop() {
assertInvariant();
Integer value = top >= 0 ? values[top] : null;
if (top > -1) {
top--;
}
assertInvariant();
return value;
}
/** Pushes the given value on the stack. */
public void push(int value) {
assertInvariant();
// ADDED: precondition
if (isFull()) {
throw new IllegalStateException("Cannot push to a full stack");
}
top++;
values[top] = value;
assertInvariant();
}
// ADDED: method for callers to check if they can call `push`
/** Checks whether the stack is full. */
public boolean isFull() {
return top == values.length - 1;
}
// ADDED: Invariant checking method
private void assertInvariant() {
if (!(-1 <= top && top < values.length)) {
throw new AssertionError("Invariant does not hold");
}
}
}
public static void main(String[] args) {
useStack(new IntStack(4));
}
// IntStack usage example
private static void useStack(IntStack stack) {
stack.push(1);
stack.pop();
stack.pop();
}
}