Advertisement
Guest User

cucu

a guest
Nov 28th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. import java.util.EmptyStackException;
  2.  
  3.  
  4. public class ListeBilatere<T> implements Liste<T>
  5. {
  6. private static class Maillon<T>
  7. {
  8. private T val; // valeur du maillon
  9. private Maillon<T> suivant; // reference au maillon suivant
  10. private Maillon<T> precedent; // reference au maillon précédent
  11. public Maillon() // utilise pour créer la sentinelle
  12. {
  13. val = null;
  14. suivant = this;
  15. precedent = this;
  16. }
  17. public Maillon(T x, Maillon<T> pred, Maillon<T> succ)
  18. {
  19. this.val = x;
  20. this.suivant = succ;
  21. this.precedent = pred;
  22. pred.suivant = this;
  23. suivant.precedent = this;
  24. }
  25. public String toString()
  26. {
  27. return this.val.toString() ;
  28. }
  29. } // classe Maillon
  30. private class Curseur
  31. {
  32. private int pos; // position courante dans la liste
  33. private Maillon<T> maillon; // maillon courant pointe
  34. private Curseur()
  35. {
  36. this.initCurseur() ;
  37. }
  38. private int position()
  39. {
  40. return 0;
  41. }
  42. private void initCurseur()
  43. {
  44. this.pos = -1;
  45. this.maillon = ListeBilatere.this.sentinelle;
  46. }
  47. private boolean estAvantPremier()
  48. {
  49. return (this.pos<0);
  50. }
  51. private boolean estApresDernier()
  52. {
  53. return (this.pos>nbVal);
  54. }
  55. private void avancer()
  56. {
  57. if(this.estApresDernier())
  58. {
  59. throw new IndexOutOfBoundsException();
  60. }
  61. else
  62. {
  63. this.pos++;
  64. this.maillon = maillon.suivant;
  65. }
  66. }
  67. private void positionner(int pos)
  68. {
  69. int p = this.pos;
  70. if(p > pos)
  71. {
  72. while(p>pos)
  73. {
  74. this.reculer();
  75. }
  76. }
  77. else
  78. {
  79. while(p<pos)
  80. {
  81. this.avancer();
  82. }
  83.  
  84. }
  85. }
  86. private void reculer()
  87. {
  88.  
  89. if(this.estAvantPremier())
  90. {
  91. throw new IndexOutOfBoundsException();
  92. }
  93. else
  94. {
  95. this.pos--;
  96. this.maillon = maillon.precedent;
  97. }
  98. }
  99. } // class Curseur
  100. private int nbVal; // nombre actuel d'éléments dans la liste
  101. private Maillon<T> sentinelle; // maillon
  102. private Curseur curs; // curseur specifique à ListeBilatere
  103.  
  104. public ListeBilatere()
  105. {
  106. sentinelle = new Maillon();
  107. nbVal = 0;
  108. curs = new Curseur();
  109. }
  110. public void inserer(int pos, T val)
  111. {
  112. if(pos<0 || pos>nbVal)
  113. {
  114. throw new IndexOutOfBoundsException();
  115. }
  116. else
  117. {
  118. curs.positionner(pos);
  119. Maillon<T> m = new Maillon<T>(val, curs.maillon.precedent, curs.maillon);
  120. nbVal ++;
  121. }
  122. }
  123.  
  124. public void enlever(int pos)
  125. {
  126. if(pos<0 || pos >nbVal)
  127. {
  128. throw new IndexOutOfBoundsException();
  129. }
  130. else
  131. {
  132. curs.positionner(pos);
  133. curs.maillon.precedent.suivant = curs.maillon.suivant;
  134. curs.maillon.suivant.precedent = curs.maillon.precedent;
  135. nbVal --;
  136. }
  137.  
  138. }
  139. /*private void insererAvantCurseur(T x) //ajoute avant le curseur
  140. {
  141.  
  142. }
  143. private void enlever() // enleverSuivant
  144. {
  145.  
  146. }*/
  147. public boolean estVide()
  148. {
  149. return longueur() == 0;
  150. }
  151. public boolean estPleine()
  152. {
  153. return false;
  154. // sinon il faudrait gerer le fait que la memoire
  155. // puisse etre saturee
  156. }
  157. public int longueur()
  158. {
  159. return this.nbVal;
  160. }
  161. public T elem(int pos)
  162. {
  163. curs.positionner(pos);
  164. return curs.maillon.val;
  165. }
  166.  
  167. public void remplacer(int pos, T x)
  168. {
  169. curs.positionner(pos);
  170. curs.maillon.val =x;
  171. }
  172. public String toString()
  173. {
  174. String res ="< ";
  175. int i=0;
  176. while(curs.position()<nbVal -1)
  177. {
  178.  
  179. curs.positionner(i);
  180. res= res + curs.maillon.toString() +", ";
  181. }
  182. curs.positionner(i);
  183. res= res + curs.maillon.toString() +" >";
  184. return res;
  185. }
  186. }//fin classe ListeBilatere
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement