-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayStack.java
More file actions
38 lines (34 loc) · 984 Bytes
/
ArrayStack.java
File metadata and controls
38 lines (34 loc) · 984 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
public class ArrayStack <T> implements StackInterface {
private T[] stack;
private static final int MAX_STACK = 100;
private int head;//Points to the first null element
public ArrayStack() {
init(MAX_STACK);
}//ArrayStack
public ArrayStack(int size) {
init(size);
}//ArrayStack
private void init(int size) {
if(size <= 0) return;
stack = (T[])(new Object[size]);
head = 0;
}//init
public void push(Object data){//Adding an element
if(head >= stack.length) return; //Stack is full
stack[head] = (T)data;
head++;
}//push
public T pop(){
if(head == 0) return null;//Empty
return stack[--head];//Skips having the tempval and extra memory with one liner
}//pop
public T peek(){
if(head <= 0) return null;
return stack[head-1];
}//peek
public void print(){//Queue goes in reverse order
for(int i = head; i >= 0; i--) {
System.out.println(stack[i].toString());
}//for
}//print
}//ArrayStack