Simply Linked List Java

1

I am desperate to find help on linked lists in java; I've searched everywhere for examples, videos, I've already read a couple of books and I can not understand how they work. What I do not understand most is WHERE the new elements of the list that are being added are being saved, in start variable or end variable

class 1

package prueba.lista;

public class Lista {

Nodo inicio;
Nodo fin;

    public Lista()
    {
        inicio = null;
        fin = null;
    }

    public void InsertarFinal( String info)
    {
        Nodo nuevo = new Nodo( info, null);

        if (inicio == null)
        {
            fin = nuevo;
            inicio = nuevo;
        }

        else 
        {   

            fin.setSiguiente(nuevo);  <-------------------------------------
            fin = nuevo;  // esta es la linea de codigo que no comprendo,  ^
                          //porque al asignarle nuevo a fin, a mi parecer  |
                          //se borra lo que estaba en fin anteriormente y  |
                          //ahora tendria unicamente nuevo; y la linea     |
                          //anterior de codigo           ----------------->
                          //no seriviría de nada porque la variable fin
                          //se sobreescribío completamente
        }
    }

    public void Mostrar()
    {
        Nodo temp = inicio;    //esto es lo otro que no entiendo, donde se 
                               //guardaron todos los elementos? en variable inicio?
                               //como es eso posible? la unica vez que se le 
                               //asignó algo a variable inicio es cuando se 
                              // validaba que era null, entonces se le asignaba
                              // el primer elemento de la lista
        while( temp != null)
        {
            System.out.println( temp.getInfo() );
            temp = temp.siguiente;
        }
    }
}

class 2

package prueba.lista;


public class Nodo {

    private String info;
    Nodo siguiente;

    public Nodo( String i, Nodo s)
    {
        info = i;
        siguiente = s;
    }


    public String getInfo() {
        return info;
    }


    public void setSiguiente(Nodo siguiente) {
        this.siguiente = siguiente;
    }


}

Class 3

package prueba.lista;

public class PruebaLista {

   public static void main(String[] args) {
        Lista l1 = new Lista();

        l1.InsertarFinal("a");
        l1.InsertarFinal("b");
        l1.InsertarFinal("c");
        l1.InsertarFinal("d");

        l1.Mostrar();
    }

}

help please: (

    
asked by Abner 30.03.2017 в 22:41
source

1 answer

3

Visualize your structure like this:

Inicio-->Nodo1(sig)-->Nodo2(sig)-->Nodo3(sig)<--Fin

You have two pointers, start and end .

  • home points to your first node
  • end points to the last node added, and is updated each time a new node is added

In the example above fin points to Node3. if we call InsertarFinal , a new node is created, Node4, this node is added to Node3 (the node pointed to by fin at this moment) as follows, and finally the reference is changed from fin to Node4 to again point to the last node. The result is:

Inicio-->Nodo1(sig)-->Nodo2(sig)-->Nodo3(sig)-->Nodo4(sig)<--Fin

In Mostrar a pointer temp is created pointing to the first node, Node1. After printing the node info, it is checked if the node has a following node. For Node1 that is Node2. Then we change the pointer temp to the next node (Node2), repeating the process until it reaches Node4, which does not have any next node assigned.

    
answered by 31.03.2017 в 01:50