APL/DEV 3.2/TP04/Chaine/ChainePile.java

48 lines
942 B
Java
Raw Permalink Normal View History

2022-10-26 11:13:11 +02:00
public class ChainePile<E> implements Pile<E> {
private Chaine<E> lastChain;
public ChainePile() {}
@Override
public E getFirst() {
if (lastChain != null) {
return lastChain.getValue();
} else {
return null;
}
}
@Override
public E pollFirst() {
if (lastChain != null) {
E value = lastChain.getValue();
lastChain = lastChain.getNext();
return value;
} else {
return null;
}
}
@Override
public void addFirst(E e) {
Chaine<E> link = new Chaine<E>();
link.setValue(e);
link.setNext(lastChain);
lastChain = link;
}
@Override
public int size() {
int count = 0;
Chaine<E> curr = lastChain;
while (curr != null) {
curr = curr.getNext();
count++;
}
return count;
}
}