Answer to Question #257768 in C for Lucifer

Question #257768

void deleteNodeFromStart(struct node_d * head) complete this function for circular doubly linked list.


1
Expert's answer
2021-10-28T05:22:32-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS