I'm learning data structures and was practicing questions on circular linked list. So, i'm supposed to write a function which deletes the head
node of a circular linked list, so i came up with this code -
void del_head(struct node** head, struct node** tail)
{
struct node* new_head = (*head)->next;
free(*head);
*head = new_head;
(*tail)->next = *head;
}
After debugging i found that the tail
is not getting updated.
I'm having tough time trying to find the problem.
Any help is appreciated, thanks
Your code will work perfectly fine for a circular list having more than
1
node and since you are deletinghead
node, so if the list will have more than1
node,tail
will be pointing to same node afterhead
node deletion as it was pointing to a node before deletion.Consider a case where the circular list has only one node:
head
,tail
and nodenext
all are pointing to same node.For this scenario, your code will end up accessing a deallocated memory.
You can do: