Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. package pt.ipleiria.estg.dei.aed.colecoes.iteraveis.lineares.naoordenadas.estruturas;
  2.  
  3. import pt.ipleiria.estg.dei.aed.colecoes.iteraveis.IteradorIteravelDuplo;
  4. import pt.ipleiria.estg.dei.aed.colecoes.iteraveis.lineares.naoordenadas.ColecaoIteravelLinearNaoOrdenada;
  5.  
  6. import java.io.Serializable;
  7. import java.util.NoSuchElementException;
  8.  
  9. public class ListaDuplaCircularBaseNaoOrdenada<T> implements ColecaoIteravelLinearNaoOrdenada<T> {
  10.  
  11. protected No base;
  12. protected int numeroElementos;
  13.  
  14. protected class No implements Serializable {
  15. private static final long serialVersionUID = 1;
  16.  
  17. protected No seguinte;
  18. protected No anterior;
  19. protected T elemento;
  20.  
  21. public No() {
  22. this.elemento = null;
  23. this.anterior = this;
  24. this.seguinte = this;
  25. }
  26.  
  27. public No(T elemento, No seguinte) {
  28. this.elemento = elemento;
  29. this.seguinte = seguinte;
  30. this.anterior = seguinte.anterior;
  31.  
  32. seguinte.anterior = anterior.seguinte = this;
  33. }
  34. }
  35.  
  36. protected class Iterador implements IteradorIteravelDuplo<T>{
  37.  
  38. protected No atual;
  39.  
  40. public Iterador() {
  41. reiniciar();
  42. }
  43.  
  44. @Override
  45. public void reiniciar() {
  46. this.atual = base;
  47. }
  48.  
  49. @Override
  50. public boolean podeRecuar() {
  51. if (atual.anterior != base){
  52. return true;
  53. }else{
  54. return false;
  55. }
  56. }
  57.  
  58. @Override
  59. public T recuar() {
  60. if (!podeRecuar()){
  61. return null;
  62. }
  63. atual = atual.anterior;
  64. return atual.elemento;
  65. }
  66.  
  67. @Override
  68. public T corrente() {
  69. if (!(atual == base)){
  70. return atual.elemento;
  71. }
  72. return atual.elemento;
  73. }
  74.  
  75. @Override
  76. public boolean podeAvancar() {
  77. if (atual.seguinte != base){
  78. return true;
  79. }else{
  80. return false;
  81. }
  82. }
  83.  
  84. @Override
  85. public T avancar() {
  86. if (!podeAvancar()){
  87. return null;
  88. }
  89. atual = atual.seguinte;
  90. return atual.elemento;
  91. }
  92. }
  93.  
  94. public ListaDuplaCircularBaseNaoOrdenada() {
  95. base = new No();
  96. this.numeroElementos = 0;
  97. }
  98.  
  99. protected No getNo(int indice){
  100. if (indice < 0 || indice >= numeroElementos) {
  101. throw new IndexOutOfBoundsException("Indice inválido");
  102. }
  103. No auxiliar = null;
  104. int contador = 0;
  105. if (indice < numeroElementos/2){ //procurar do primeiro para o ultimo
  106. auxiliar = base.seguinte;
  107. for (int i = 0; i < indice; i++) {
  108. auxiliar = auxiliar.seguinte;
  109. contador++;
  110. if (contador == indice) {
  111. return auxiliar;
  112. }
  113. }
  114. }else{ //procurar do ultimo para o primeiro
  115. auxiliar = base.anterior;
  116. contador = numeroElementos;
  117. for (int i = numeroElementos-1; i > indice; i--) {
  118. auxiliar = auxiliar.anterior;
  119. contador--;
  120. if (contador == indice) {
  121. return auxiliar;
  122. }
  123. }
  124. }
  125. return null;
  126. }
  127.  
  128. private No getNo(T elem){
  129. No auxiliar = base.seguinte;
  130. while(auxiliar != null){
  131. if (auxiliar.elemento.equals(elem)){
  132. return auxiliar;
  133. }
  134. auxiliar = auxiliar.seguinte;
  135. }
  136. throw new NoSuchElementException("Elemento não existe");
  137. }
  138.  
  139. private No getNoPorRefencia(T elem){
  140. No auxiliar = base.seguinte;
  141. while(auxiliar != null){
  142. if (auxiliar.elemento == elem){
  143. return auxiliar;
  144. }
  145. auxiliar = auxiliar.seguinte;
  146. }
  147. throw new NoSuchElementException("Elemento não existe");
  148. }
  149.  
  150. private T removerNo(No no) {
  151. no.anterior.seguinte = no.seguinte;
  152. no.seguinte.anterior = no.anterior;
  153. return no.elemento;
  154. }
  155.  
  156. @Override
  157. public void inserirNoInicio(T elem) {
  158. new No(elem, base.seguinte);
  159. numeroElementos++;
  160. }
  161.  
  162. @Override
  163. public void inserirNoFim(T elem) {
  164. new No(elem, base);
  165. numeroElementos++;
  166. }
  167.  
  168. @Override
  169. public void inserir(int indice, T elem) {
  170. if (indice == numeroElementos){
  171. inserirNoFim(elem);
  172. }else{
  173. new No(elem, getNo(indice));
  174. numeroElementos++;
  175. }
  176. }
  177.  
  178. @Override
  179. public T removerDoInicio() {
  180. numeroElementos--;
  181. return removerNo(base.seguinte);
  182. }
  183.  
  184. @Override
  185. public T removerDoFim() {
  186. numeroElementos--;
  187. return removerNo(base.anterior);
  188. }
  189.  
  190. @Override
  191. public T remover(T elem) {
  192. numeroElementos--;
  193. return removerNo(getNo(elem));
  194. }
  195.  
  196. @Override
  197. public T remover(int indice) {
  198. numeroElementos--;
  199. return removerNo(getNo(indice));
  200. }
  201.  
  202. @Override
  203. public T removerPorReferencia(T elem) {
  204. numeroElementos--;
  205. return removerNo(getNoPorRefencia(elem));
  206. }
  207.  
  208. @Override
  209. public T consultar(int indice) {
  210. return getNo(indice).elemento;
  211. }
  212.  
  213. @Override
  214. public boolean contem(T elem) {
  215. try{
  216. getNo(elem);
  217. return true;
  218. }catch(Exception e){
  219. return false;
  220. }
  221. }
  222.  
  223. @Override
  224. public boolean contemReferencia(T elem) {
  225. try{
  226. getNoPorRefencia(elem);
  227. return true;
  228. }catch(Exception e){
  229. return false;
  230. }
  231. }
  232.  
  233. @Override
  234. public IteradorIteravelDuplo<T> iterador() {
  235. return new Iterador();
  236. }
  237.  
  238. @Override
  239. public int getNumeroElementos() {
  240. return numeroElementos;
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement