I'm stuck on this particular function that frees all even nodes from the linked list. I've figured out how to free all the nodes from a linked list but I cannot figure this out. The code i'm posting is very wrong. What I don't understand is how to use a node *temp variable and link it to the head->next node, as the head is what is being freed (because it is even). Also, at the end of the while loop, I know that I need to increment to the next node in the list, but I seem to be doing that already in the first if statement, so wouldn't calling current = current->next actually be taking me to current->next->next, and skipping a node? Sorry for this massive block of text.
node *delete_even_node(node *head)
{
node *temp, *current = head;
if (head == NULL)
return NULL;
while (current != NULL)
{
if (current->data % 2 == 0)
{
printf("Deleting Even %d\n", current->data);
temp = current->next; //problem starts
temp = temp->next;
free(temp);
}
else
current = current->next;
current = current->next;
}
return head;
}
nextfield of the preceding node. - M Oehmcurrent, not the node two places after it. (And Ifcurrent->next == NULL, the assignmenttemp = temp->nextwill crash.) - M Oehm