-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayStack.java
66 lines (59 loc) · 1.48 KB
/
ArrayStack.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.nio.*;
import java.util.concurrent.locks.*;
// Array stack is a bounded lock-based stack using an
// array. It uses a common lock for both push and pop
// operations.
class ArrayStack<T> {
Lock lock;
T[] data;
int top;
// lock: common lock for push, pop
// data: array of values in stack
// top: top of stack (0 if empty)
@SuppressWarnings("unchecked")
public ArrayStack(int capacity) {
lock = new ReentrantLock();
data = (T[]) new Object[capacity];
top = 0;
}
// 1. Lock stack.
// 2. Try push.
// 3. Unlock stack.
public void push(T x) throws BufferOverflowException {
try {
lock.lock(); // 1
tryPush(x); // 2
} finally {
lock.unlock(); // 3
}
}
// 1. Lock stack.
// 2. Try pop.
// 3. Unlock stack.
public T pop() throws BufferUnderflowException {
try {
lock.lock(); // 1
return tryPop(); // 2
} finally {
lock.unlock(); // 3
}
}
// 1. Ensure stack is not full
// 2. Save data at top.
// 3. Increment top.
protected void tryPush(T x)
throws BufferOverflowException {
if (top == data.length) // 1
throw new BufferOverflowException(); // 1
data[top++] = x; // 2, 3
}
// 1. Ensure stack is not empty.
// 2. Decrement top.
// 3. Return data at top.
protected T tryPop()
throws BufferUnderflowException {
if (top == 0) // 1
throw new BufferUnderflowException(); // 1
return data[--top]; // 2, 3
}
}