APL/DEV 3.2/TP04/Tableau/ArrayPile.java

56 lines
1.0 KiB
Java
Raw Normal View History

2022-10-26 11:13:11 +02:00
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;
}
}