-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoundedStack.java
More file actions
83 lines (71 loc) · 2.26 KB
/
BoundedStack.java
File metadata and controls
83 lines (71 loc) · 2.26 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
/**
* An abstract class that represents a stack of integers.
* All methods should be implemented in a thread-safe manner.
*/
/*
* You must extend this class and implement the abstract methods in
* such a fashion that all race conditions are avoided.
*/
public abstract class BoundedStack {
/*
Protected instance fields. Use these in your subclass's implementation.
*/
/**
* The maximum number of elements the stack can hold at a time.
* Initialized in the constructor.
*/
protected final int capacity;
/**
* An array holding the items on the stack.
*/
protected final int[] elements;
/**
* Index of the next unoccupied location on the stack.
*/
protected int top;
/**
* Constructor for a new BoundedStack object.
* @param capacity : The capacity of this stack.
*/
/*
Override this constructor, and invoke super(), passing in the
capacity.
*/
public BoundedStack(int capacity) {
this.capacity = capacity;
this.elements = new int[capacity]; // Defaults to null value
this.top = 0;
}
/**
* Pushes the provided element onto the top of the stack.
* @param t : The item to push.
* @throws InterruptedException
*/
public abstract void push(int t) throws InterruptedException;
/**
* Returns the value on top of the stack.
* @return : The value on top of the stack.
*/
public abstract int peek() throws InterruptedException;
/**
* Removes the top value from the stack and returns it.
* @return : The item popped from the stack.
* @throws InterruptedException
*/
public abstract int pop() throws InterruptedException;
/**
* Returns true if the stack is empty and false otherwise.
* @return : Whether the stack is empty.
*/
public abstract boolean isEmpty();
/**
* Returns true if the stack is full, and false otherwise.
* @return : Whether or not the stack is full.
*/
public abstract boolean isFull();
/**
* Returns the number of elements currently on the stack.
* @return : The size of the stack.
*/
public abstract int getSize();
}