56 lines
1.0 KiB
Java
56 lines
1.0 KiB
Java
public class ArrayPile<E> implements Pile<E>{
|
|
|
|
int size = 16;
|
|
int currentElement = 0;
|
|
|
|
E[] table;
|
|
|
|
public ArrayPile() {
|
|
table = (E[])new Object[size];
|
|
};
|
|
|
|
private void resizeStack() {
|
|
size *= 2;
|
|
E[] newTable = (E[])new Object[size];
|
|
|
|
for (int i = 0; i < table.length; i++) {
|
|
newTable[i] = table[i];
|
|
}
|
|
|
|
table = newTable;
|
|
}
|
|
|
|
@Override
|
|
public E getFirst() {
|
|
if (currentElement == 0) {
|
|
return null;
|
|
} else {
|
|
return table[currentElement-1];
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public E pollFirst() {
|
|
if (currentElement == 0) {
|
|
return null;
|
|
} else {
|
|
E e = table[currentElement-1];
|
|
currentElement--;
|
|
return e;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void addFirst(E e) {
|
|
if (currentElement >= table.length) resizeStack();
|
|
table[currentElement] = e;
|
|
currentElement++;
|
|
}
|
|
|
|
@Override
|
|
public int size() {
|
|
return currentElement;
|
|
}
|
|
|
|
}
|