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
| public class ListeCellulesCirculaire<X>{
private CelluleCirculaire<X> debut;
//Liste vide
ListeCellulesCirculaire(CelluleCirculaire<X> debut){
this.debut = debut;
}
public void ajouterDebut(CelluleCirculaire<X> cellule){
cellule.setCelluleSuivante(this.debut);
if(this.debut != null)
debut.setCellulePrecedente(cellule);
debut = cellule;
cellule.setCellulePrecedente(null);
}
public void ajouterApres(CelluleCirculaire<X> precedent, CelluleCirculaire<X> cellule){
if(precedent == null){
ajouterDebut(cellule);
}else{
cellule.setCelluleSuivante(precedent.getCelluleSuivante());
cellule.setCellulePrecedente(precedent);
if(precedent.getCelluleSuivante() != null){
precedent.getCelluleSuivante().setCellulePrecedente(cellule);
}
precedent.setCelluleSuivante(cellule);
}
}
public void supprimer(CelluleCirculaire<X> cellule){
if(cellule == debut){
debut = cellule.getCelluleSuivante();
}else{
//cas cellule précédente n'est pas null
cellule.getCellulePrecedente().setCelluleSuivante(cellule.getCelluleSuivante());
}
if(cellule.getCelluleSuivante() != null){
cellule.getCelluleSuivante().setCellulePrecedente(cellule.getCellulePrecedente());
}
}
//Renvoie la longueur de la liste
public int longueur(){
int longueur = 0;
CelluleCirculaire<X> c = this.debut;
while (c!=null) {
longueur = longueur + 1;
c = c.getCelluleSuivante();
}
return longueur;
}
//Renvoie une chaîne de caractères représentant la liste
public String toString(){
String s = "[";
CelluleCirculaire<X> c = this.debut;
while (c!=null) {
s = s + c.getValeur();
c = c.getCelluleSuivante();
if (c!=null) s = s +"; ";
}
s = s + "]";
return s;
}
public CelluleCirculaire<X> getDebut() {
return debut;
}
public void setDebut(CelluleCirculaire<X> debut) {
this.debut = debut;
}
} |
Partager