-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab1BArrayStack.java
More file actions
61 lines (48 loc) · 1.4 KB
/
Copy pathLab1BArrayStack.java
File metadata and controls
61 lines (48 loc) · 1.4 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
public class ArrayStack implements Stack {
private final int DEFAULT_STACK_SIZE = 10;
private int topOfStackIndex;
private Object[] objArray;
public ArrayStack(int sizeStack) {
objArray = new Object[sizeStack];
topOfStackIndex = 0;
}
public ArrayStack() {
objArray = new Object[DEFAULT_STACK_SIZE];
topOfStackIndex = 0;
}
public void push(Object element) {
if (!isEmpty()) //should be more like if objArray.length = sizq... resize
expandCapacity();
objArray[topOfStackIndex] = element;
topOfStackIndex++;
}
public Object peek() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("Array is Empty");
return objArray[topOfStackIndex-1];
}
public Object pop() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("Array is Empty");
Object temp = peek();
topOfStackIndex--;
return temp;
}
public boolean isEmpty() {
return (topOfStackIndex == 0)
}
public int size() {
return topOfStackIndex;
}
//public String toString() {
//return(this.toString());
//}
private void expandCapacity() {
int newSize = (size()*2);
Object[] expandArrayStack = new Object[newSize];
for (int i = 0; i < size(); i++){
expandArrayStack[i] = objArray[i];
}
objArray = expandArrayStack;
}
}