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--;
}