void deleteNodeFromStart(struct node_d * head) complete this function for circular doubly linked list.
#include <stdio.h>
#include <stdlib.h> // free & malloc
typedef struct node_d {
int v;
struct node_d *next;
struct node_d *prev;
} node_d;
// circular doubly linked list
void deleteNodeFromStart(node_d ** head) {
if (!*head) return;
node_d *prev = (*head)->prev;
if (prev == *head) {
// only 1 element in the lis
free(*head);
*head = NULL;
return;
}
node_d *next = (*head)->next;
if (next == prev) {
// only 2 elements in the list
free(*head);
*head = prev;
// self-linked
(*head)->next = *head;
(*head)->prev = *head;
return;
}
// otherwise there are more than 2 elements in the list
prev->next = next;
next->prev = prev;
free(*head);
*head = next;
}
Comments
Leave a comment