-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathObjectStack.java
More file actions
44 lines (33 loc) · 887 Bytes
/
ObjectStack.java
File metadata and controls
44 lines (33 loc) · 887 Bytes
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
package StackArray;
import java.util.Arrays;
/**
* Expand the ArrayList implementation of stack here to use an Object[] array. Still implement push, pop, and isEmpty.
* Remember, you might need to resize the stack in the push method.
* @param <E>
*/
public class ObjectStack<E> {
private Object[] elements = new Object[10];
int size = 0;
public ObjectStack() {
}
public void push(E item) {
if (elements.length == size) {
elements = Arrays.copyOf(elements, elements.length + 5);
} else {
elements[size] = item;
size++;
}
}
public E pop() {
E last = (E) elements[size-1];
elements[size -1] = null;
size --;
return last;
}
public boolean isEmpty(){
if(size == 0) {
return true;
}else
return false;
}
}