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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
public class PileListe implements PileI, Cloneable {
private Maillon stk;
private int capacite;
private int nombre;
private static class Maillon implements Cloneable {
private Object element;
private Maillon suivant;
public Maillon(Object element, Maillon suivant) {
this.element = element;
this.suivant = suivant;
}
public Maillon suivant() {
return this.suivant;
}
public Object element() {
return this.element;
}
public Object clone() throws CloneNotSupportedException {
Maillon m = (Maillon) super.clone();
m.element = element;
return m;
}
}
public PileListe(int taille) {
if (taille <= 0)
taille = CAPACITE_PAR_DEFAUT;
this.stk = null;
this.capacite = taille;
}
public PileListe() {
this(PileI.CAPACITE_PAR_DEFAUT);
}
public void empiler(Object o) throws PilePleineException {
if (estPleine())
throw new PilePleineException();
stk = new Maillon (o,stk);
nombre = nombre + 1 ;
}
public Object depiler() throws PileVideException {
if (estVide())
throw new PileVideException();
if (stk != null) {
Object o = stk.element();
stk = stk.suivant();
return o;
}
else return null;
}
public Object sommet() throws PileVideException {
if (estVide())
throw new PileVideException();
return stk.element ;
}
public boolean estVide() {
return stk == null ;
}
public boolean estPleine() {
return this.taille() >= capacite ;
}
// ici je coince un peu
public String toString() {
String s = "[";
Stack temp = new Stack();
while (!this.estVide()) {
temp.push(stk.element);
s = s + stk.element;
}
return s + "]";
}
//...
public int capacite() {
return this.capacite;
}
//...
public int taille() {
return nombre;
}
} |
Partager