-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathArrayBasedStack.java
40 lines (40 loc) · 1.1 KB
/
ArrayBasedStack.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
public class ArrayBasedStack {
private Object[] array; // Container for Stack elements
private int top; // Index of top element
// Constructor for creating stack of given capacity
public ArrayBasedStack(int capacity) {
array = new Object[capacity];
top = -1;
}
// Method for adding a new element to top of stack
public void push(Object obj) throws Exception {
if(size() == array.length) {
throw new Exception("Stack is full!");
}
top++; // Advance to next cell
array[top] = obj; // Add new element
}
// Method for removing element from top of stack
public Object pop() throws Exception {
if(isEmpty()) {
throw new Exception("Stack is empty!");
}
Object toReturn = array[top]; // Element to return
array[top] = null; // Replace it with null
top--; // Update top to point to new top
return toReturn;
}
// Method for getting top element without removing it
public Object top() throws Exception {
if(isEmpty()) {
throw new Exception("Stack is empty!");
}
return array[top];
}
public int size() {
return top + 1;
}
public boolean isEmpty() {
return (top == -1);
}
}