package p4a; public class LinkedListPerso<T> implements Structure { private static class Maillon<T> { private T val; // valeur du maillon private Maillon<T> suivant; // reference au maillon suivant private Maillon() // pour creer la sentinelle { val = null; suivant = this; } private Maillon(T x, Maillon<T> succ) { val = x; suivant = succ; } } // class Maillon private class Curseur { private int pos; // position courante dans la liste private Maillon<T> maillon; // maillon courant pointe private Curseur() { pos = -1; maillon = LinkedListPerso.this.sentinelle; } private void positionner(int pos) { if (!( pos >= -1 && pos <= longueur())) throw new IllegalStateException( "mauvaise position pos = " + pos + " "); if (pos < this.pos) { this.pos = -1; maillon = sentinelle; } while (this.pos < pos) { this.pos++; maillon = maillon.suivant; } } } // class Curseur private Maillon<T> sentinelle; private Curseur curs; // curseur specifique a ListeChainee private int nbVal; public LinkedListPerso() { sentinelle = new Maillon<T>() ; curs = this.new Curseur(); nbVal = 0; } public int longueur() { return nbVal; } public boolean estVide() { return nbVal == 0; } @Override public T acces(int pos) { if (!(pos >= 0 && pos < nbVal)) throw new IndexOutOfBoundsException( String.valueOf(pos)); curs.positionner(pos); return curs.maillon.val; } public void remplacer(int pos, T x) { if (!(pos >= 0 && pos < nbVal)) throw new IndexOutOfBoundsException( String.valueOf(pos)); curs.positionner(pos) ; curs.maillon.val = x; } @Override public void ajout(Object element, int position) { if (!(position >= 0 && position <= nbVal)) throw new IndexOutOfBoundsException( String.valueOf(position)); curs.positionner(position - 1) ; Maillon<T> pred = curs.maillon; Maillon<T> succ = pred.suivant; Maillon<T> nouveau = new Maillon<T>((T)element, succ); pred.suivant = nouveau ; nbVal++; } @Override public void suppression(int pos) { if (!(pos >= 0 && pos < nbVal)) throw new IndexOutOfBoundsException( String.valueOf(pos)); curs.positionner(pos - 1); Maillon<T> pred = curs.maillon; Maillon<T> aEnlever = pred.suivant; Maillon<T> succ = aEnlever.suivant; pred.suivant = succ; nbVal--; } } // class ListeChainee