What is the next algorithm for?

1
public void eliminar(int index){

    if(index==0){

        cabeza=cabeza.obtenerSiguiente();

    }else{

        int contador=0;

        Nodo temporal=cabeza;

        while(contador<index-1){

            temporal=temporal.obtnerSiguiente();

            contador++;
        }

        temporal.enlazarSiguiente(temporal.obtenerSiguiente().obtenerSiguiente());
    }

    size--;
    
asked by Stephanie B Bautista 03.03.2017 в 05:32
source

1 answer

8

Remove the item from the desired position, as follows: Assume that [x] is an element of the chained list (Node).

Suppose we have the following node structure:

[1] - [2] - [3] - [4] - [5] - [6] Suppose the following call occurs: delete (0);

public void eliminar(int index)
{
    if(index==0)
    {
        // cabeza = [1]-[2]-[3]-[4]-[5]-[6];
        cabeza=cabeza.obtenerSiguiente();
        // cabeza = [2]-[3]-[4]-[5]-[6];
    }
    else
    ...
    size--;
}

Now suppose that the next entry: delete (3);

public void eliminar(int index)
{
    ...
    else
    {
        int contador = 0;
        Nodo temporal = cabeza;
        // temporal = [1]-[2]-[3]-[4]-[5]-[6];
        // Cada vuelta: 0 < 3 - 1(TRUE) // 1 < 3 - 1(TRUE) // 2 < 3 - 1(FALSE)
        while(contador<index-1)
        {
            //realización de este codigo 2 tiempos
            temporal=temporal.obtnerSiguiente();
            // temporal en la primera ronda = [2]-[3]-[4]-[5]-[6];
            // temporal en la segunda ronda = [3]-[4]-[5]-[6];
            // cabeza = [1]-[2]-[3]-[4]-[5]-[6]
            contador++;
        }
        // contador = 2;
        // temporal.obtenerSiguiente() = [4]-[5]-[6]
        // temporal.obtenerSiguiente().obtenerSiguiente() = [5]-[6]
        temporal.enlazarSiguiente(temporal.obtenerSiguiente().obtenerSiguiente());
        // temporal.enlazarSiguiente... = [3]-(Eliminado)-[5]-[6]
        // cabeza = [1]-[2]-[3]-[5]-[6]
    }

    size--;
}
    
answered by 03.03.2017 в 06:21