I'm supposed to define a function that removes all nodes with a specified value from a linked list.
I have to use this structure:
typedef struct LIST
{
int Object;
struct LIST *Next;
} LIST;
and here is the function I wrote
void LDeleteElem(LIST **List, int Elem)
{
LIST* Curr = *List;
LIST* Prev = NULL;
while (Curr != NULL)
{
if (Curr->Object == Elem) /*found the node*/
{
if (Prev == NULL)
*List = Curr->Next; /*if found at the beginning*/
else
Prev->Next = Curr->Next; /*if found anywhere else*/
free(Curr);
}
Prev = Curr; Curr = Curr->Next;
}
}
And it crashes... If I add return;
after free(Curr);
it works fine and removes the first node with the value of Elem it finds, but I need to remove all of them, what am I doing wrong?