I'm trying to delete a data from my list but it turns out that the next value of the number I want to delete is deleted.
For example, I have on my list:
7 0 5 18 2
I want to delete the 0
, and that when viewing is:
7 5 18 2
With the code that I have, it looks like this:
7 0 18 2
The following value of 0
is erased, that is, 5
.
Code:
printf("\n\nNumero de la lista a borrar: ");//Valor del nodo a borrar
scanf("%d",&numeroABorrar);
anterior = primero;
while ((anterior->dato != numeroABorrar) && (anterior != NULL))
{
anterior = anterior->siguiente;
}
if (anterior != NULL)
{
aux = anterior->siguiente;
anterior->siguiente = aux->siguiente;
free(aux);
}