48 lines
942 B
Java
48 lines
942 B
Java
|
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;
|
||
|
}
|
||
|
|
||
|
}
|