function to delete all nodes at even position in C
// function to delete a node in a Doubly Linked List.
// head_ref --> pointer to head node pointer.
// del --> pointer to node to be deleted
void deleteNode(Node** head_ref, Node* del)
{
// base case
if (*head_ref == NULL || del == NULL)
return;
// If node to be deleted is head node
if (*head_ref == del)
*head_ref = del->next;
// Change next only if node to be
// deleted is NOT the last node
if (del->next != NULL)
del->next->prev = del->prev;
// Change prev only if node to be
// deleted is NOT the first node
if (del->prev != NULL)
del->prev->next = del->next;
// Finally, free the memory occupied by del
free(del);
return;
}
// function to delete all the nodes at even position
// from the doubly linked list
void deleteEvenPositionNodes(Node** head_ref)
{
int i = 0;
Node* ptr = *head_ref;
Node* next;
while (ptr != NULL) {
i++;
next = ptr->next;
printf("%d\n", ptr->data);
if (i % 2 == 0)
deleteNode(head_ref, ptr);
ptr = next;
}
}
Get Answers For Free
Most questions answered within 1 hours.