Search a node data in a list

0

I am trying to find a data from my linked list, in which I scroll it until it is null and say if that number to look for or not in the list

Code:

aux=primero; 

while(aux != NULL && aux->dato != 50)
{
    aux=aux->siguiente;
}

if(aux->dato==50)
{
    printf("\n\n Numero %d esta en la lista",aux->dato);
}
else
{
    printf("\n\n El numero %d no esta en la lista",aux->dato);
}

I miss an error saying that the program stopped working.

    
asked by Mario Guiber 08.08.2017 в 22:24
source

1 answer

1

The problem is that if you are not asking him%% of% if his data is equal to 50. One solution would be:

aux=primero;
int ok = 0, numeroABuscar = 50;

while(aux != NULL && !ok)
{
    if (aux->dato == numeroABuscar) {
        ok = 1;
    } else {
        aux=aux->siguiente;
    }
}
if(ok)
{
    printf("\n\n Numero %d esta en la lista", numeroABuscar);
}
else
{
    printf("\n\n El numero %d no esta en la lista", numeroABuscar);
}
    
answered by 08.08.2017 / 22:52
source