-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArray.java
More file actions
48 lines (39 loc) · 882 Bytes
/
StackArray.java
File metadata and controls
48 lines (39 loc) · 882 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
46
47
48
public class StackArray <T> implements StackInterface<T>{ //stacks fill in last in first out; adding box of milk top top of milk stack in fridge
private T[] stack;
public static final int arrSize = 100;
private int head; //head is movable; fist null element
public StackArray() {
init(arrSize);
}
public StackArray(int aSize) {
init(aSize);
}
private void init(int aSize) {
if(aSize<=0)
return;
head = 0;
stack = (T[])(new Object[aSize]);
}
public void push(T aData) {
if(head>=stack.length)
return;
stack[head] = aData;
head++;
}
public T pop() { //removes last element added
if(head<=0)
return null;
T ret = stack[head-1];
head--;
return ret;
}
public T peek() {
if(head<=0)
return null;
return stack[head-1];
}
public void print() {
for(int i = head-1; i>=0; i--)
System.out.println(stack[i]);
}
}