-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathGenericStack.java
More file actions
47 lines (34 loc) · 998 Bytes
/
GenericStack.java
File metadata and controls
47 lines (34 loc) · 998 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
45
package StackArray;
//import com.sun.java.util.jar.pack.ConstantPool;
import java.util.Arrays;
/**
* Expand the ArrayList implementation of stack here to use an E[] array. Still implement push, pop, and isEmpty.
* Remember, you might need to resize the stack in the push method.
* @param <E>
*/
public class GenericStack<E> {
private Object[] elements;
private int top;
private final static int EMPTY = -1;
private final static int DEFAULT_Capacity = 10;
public GenericStack() {
this(DEFAULT_Capacity);
}
public GenericStack(int initialCapacity) {
elements = new Object [initialCapacity];
top = EMPTY;
}
public boolean isEmpty(){
return (top == EMPTY);
}
@SuppressWarnings("uncheck")
public E pop(){
if (top == EMPTY){
throw new IndexOutOfBoundsException();
}
return (E)elements[top--];
}
public void push(E e) {
elements[++top] = e;
}
}