Advertisement
fmbalvarez

Node + ListaEnlazada

Jun 9th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. public class Node {
  2.    
  3.     Node nodoSiguiente;
  4.     Object data;
  5.  
  6.     public Node(Object data) {
  7.         this.nodoSiguiente = null;
  8.         this.data = data;
  9.     }
  10.  
  11.     public Object getData() {
  12.         return data;
  13.     }
  14.  
  15.     public Node getSiguiente() {
  16.         return nodoSiguiente;
  17.     }
  18.  
  19.     public void setSiguiente(Node nodoSiguiente) {
  20.         this.nodoSiguiente = nodoSiguiente;
  21.     }
  22. }
  23.  
  24.  
  25.  
  26.  
  27. public class ListaEnlazada {
  28.  
  29.     private Node primerNodo;
  30.     private int elementosLista;
  31.  
  32.     /**
  33.      * post: crea una ListaEnlazada vacia
  34.      */
  35.     public ListaEnlazada() {
  36.         this.primerNodo = new Node(null);
  37.         this.elementosLista = 0;
  38.     }
  39.  
  40.     /**
  41.      * pre: recibe objeto a agregar como parametro
  42.      * post: agregar el objeto
  43.      * indicado al final de la lista
  44.      * @param o
  45.      */
  46.     public void add(Object o) {
  47.         Node nodoAAgregar = new Node(o);
  48.         Node nodoActual = primerNodo;
  49.         while (nodoActual.getSiguiente() != null) {
  50.             nodoActual = nodoActual.getSiguiente();
  51.         }
  52.         nodoActual.setSiguiente(nodoAAgregar);
  53.         elementosLista++;
  54.     }
  55.  
  56.     /**
  57.      * post: devuelve la cantidad de elementos de la lista
  58.      * @return elementosLista
  59.      */
  60.     public int size() {
  61.         return elementosLista;
  62.     }
  63.  
  64.     /**
  65.      * post: devuelve en un String los elementos que componen la lista
  66.      */
  67.     public String toString() {
  68.         Node nodo = primerNodo.getSiguiente();
  69.         String toString = "";
  70.         while (nodo != null) {
  71.             toString += " " + nodo.getData().toString() + " ";
  72.             nodo = nodo.getSiguiente();
  73.         }
  74.         return toString;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement