rambo189619

Linked List

Apr 17th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. package es.ubu.inf.edat.pr06;
  2.  
  3. import java.util.AbstractList;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.ListIterator;
  8.  
  9. public class ListaEnlazada<E> extends AbstractList<E> {
  10.     private List<E> lista;
  11.     private int contador;
  12.     private Nodo ultimo;
  13.     private Nodo primero;
  14.  
  15.     public ListaEnlazada() {
  16.         primero = null;
  17.         ultimo = null;
  18.         lista = new LinkedList<E>();
  19.         contador = 0; //Variable para saber cuantas elementos hay en la lista.
  20.     }
  21.  
  22.     @Override
  23.     public E get(int arg0) {
  24.         return lista.get(arg0);
  25.     }
  26.  
  27.     @Override
  28.     public int size() {
  29.         return contador;
  30.     }
  31.  
  32.     public boolean add(E objeto) {
  33.         Nodo nuevo = new Nodo(objeto);
  34.         if (lista.size() == 0) {
  35.             primero = nuevo;
  36.             ultimo = nuevo;
  37.         } else {
  38.             ultimo.setSiguiente(nuevo);
  39.             nuevo.setSiguiente(null);
  40.             ultimo = nuevo;
  41.         }
  42.        
  43.         contador++;
  44.         System.out.println(contador);
  45.         return true;
  46.     }
  47.  
  48.     public void add(int i, E e) {
  49.         throw new UnsupportedOperationException();
  50.     }
  51.  
  52.     public E remove(int posicion) {
  53.         E aux = ultimo.getDato();
  54.         Nodo aux2 = null;
  55.  
  56.         if (lista.size() == 0) {
  57.             throw new UnsupportedOperationException("Lista vacia");
  58.         } else if (primero == ultimo) {
  59.             primero = null;
  60.             ultimo = null;
  61.             if (posicion < 0 || posicion >= size())
  62.                 throw new IndexOutOfBoundsException();
  63.         } else {
  64.             aux2 = primero;
  65.             for (int i = 0; i < posicion; i++) {
  66.                 aux2 = aux2.getSiguiente();
  67.                 if (i == posicion - 1) {
  68.                     aux2.setSiguiente(primero);
  69.                     ultimo = aux2;
  70.                 }
  71.             }
  72.         }
  73.  
  74.         contador--;
  75.         return aux;
  76.     }
  77.  
  78.     public void clear() {
  79.         primero = null;
  80.         ultimo = null;
  81.     }
  82.  
  83.     private class Nodo {
  84.         private E dato;
  85.         private Nodo siguiente;
  86.  
  87.         public Nodo(E dato) {
  88.             this.dato = dato;
  89.         }
  90.  
  91.         public E getDato() {
  92.             return dato;
  93.         }
  94.  
  95.         public void setDato(E objeto) {
  96.             this.dato = objeto;
  97.         }
  98.  
  99.         public Nodo getSiguiente() {
  100.             return siguiente;
  101.         }
  102.  
  103.         public void setSiguiente(Nodo siguiente) {
  104.             this.siguiente = siguiente;
  105.         }
  106.  
  107.     }
  108.  
  109.     @Override
  110.     public Iterator<E> iterator() {
  111.  
  112.         return new ListIterator<E>() {
  113.             private int contador2 = 0; //Variable para saber la posicion actual.
  114.             private E aux = null;
  115.             private Nodo actual = primero;
  116.  
  117.             @Override
  118.             public boolean hasNext() {
  119.                 if (actual == null)
  120.                     return false;
  121.                 else
  122.                     return true;
  123.             }
  124.  
  125.             @Override
  126.             public E next() {
  127.                 if (contador == 0) {
  128.                     throw new UnsupportedOperationException("Lista vacia");
  129.                 } else if (primero == ultimo) {
  130.                     aux = lista.get(0);
  131.                 } else {
  132.                     if (actual!=null) {
  133.                         aux = actual.getDato();
  134.                         actual.getSiguiente();
  135.                     }
  136.  
  137.                 }
  138.  
  139.                 contador2++;
  140.                 return aux;
  141.             }
  142.  
  143.             @Override
  144.             public boolean hasPrevious() {
  145.                 if (actual == primero)
  146.                     return false;
  147.                 else
  148.                     return true;
  149.             }
  150.  
  151.             @Override
  152.             public int nextIndex() {
  153.                 return contador2;
  154.             }
  155.  
  156.             @Override
  157.             public E previous() {
  158.                 Nodo anterior = primero;
  159.  
  160.                 if (lista.size() == 0) {
  161.                     throw new UnsupportedOperationException("Lista vacia");
  162.                 } else if (primero == ultimo) {
  163.                     aux = lista.get(0);
  164.                 } else {
  165.                     if (contador < lista.size()) {
  166.                         aux = actual.getDato();
  167.                         while (anterior != actual) {
  168.                             anterior.getSiguiente();
  169.                         }
  170.                         actual.setSiguiente(anterior);
  171.                     }
  172.  
  173.                 }
  174.  
  175.                 contador2--;
  176.                 return aux;
  177.             }
  178.  
  179.             @Override
  180.             public int previousIndex() {
  181.                 return contador2;
  182.             }
  183.  
  184.             // Métodos NO utilizados
  185.             @Override
  186.             public void set(E arg0) {
  187.                 throw new UnsupportedOperationException();
  188.  
  189.             }
  190.  
  191.             @Override
  192.             public void remove() {
  193.                 throw new UnsupportedOperationException();
  194.             }
  195.  
  196.             @Override
  197.             public void add(E arg0) {
  198.                 throw new UnsupportedOperationException();
  199.             }
  200.  
  201.         };
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment