C Data Structures - Circular Singly Linked List Other Related Topics

C - Delete first node by key of the Circular Singly Linked List



In this method, first node in the circular singly linked list with specified key (value) is deleted. For example - if the given list is 10->20->30->10->20 and the first occurrence of 20 is deleted, the list becomes 10->30->10->20.

First, the head of the linked list is checked for null value. If the head is not null, the value stored in it is equal to the key, and head is the only element in the list then make the head as null. If the head is not null, the value stored in it is equal to the key, the list contains more than one element then make head next as head and update the link of last element of the list with the new head. Else, traverse to the node previous to the node with value equal to key, and adjust links.

The function pop_first is created for this purpose. It is a 4-step process.

void pop_first(struct Node** head_ref, int key) {
  
  //1. if head is not null, create two nodes- temp
  //   and nodeToDelete - to traverse and track 
  //   the node to delete  
  if(*head_ref != NULL) {
    struct Node* temp = *head_ref;
    struct Node* nodeToDelete = *head_ref;
    
    //2. if the value store at head is the key and head
    //   is the only element in the list, make it null
    if(temp->data == key) {
      if(temp->next == *head_ref) {
        *head_ref = NULL;
      } else {

        //3. if the value store at head is the key and list
        //   contains more than 1 elements, traverse to the
        //   last element of the list and link it to the new head
        while(temp->next != *head_ref) {
          temp = temp->next;
        }
        *head_ref = (*head_ref)->next;
        temp->next = *head_ref; 
        free(nodeToDelete); 
      }
    } else {
      
      //4. Else, traverse to the node previous to the 
      //   node with value equal to key, and adjust links 
      while(temp->next != *head_ref) {
        if(temp->next->data == key) {
          nodeToDelete = temp->next;
          temp->next = temp->next->next;
          free(nodeToDelete);
          break; 
        }
        temp = temp->next;
      }
    }
  }
}

The below is a complete program that uses above discussed concept to delete first occurrence of the specified key (if exists) of the circular singly linked list.

#include <stdio.h>
#include <stdlib.h>

//node structure
struct Node {
  int data;
  struct Node* next;
};

//Add new element at the end of the list
void push_back(struct Node** head_ref, int newElement) {  
  struct Node *newNode, *temp;
  newNode = (struct Node*)malloc(sizeof(struct Node)); 
  newNode->data = newElement;  
  newNode->next = NULL;
  if(*head_ref == NULL) {
    *head_ref = newNode;
     newNode->next = *head_ref;
  } else {
    temp = *head_ref;
    while(temp->next != *head_ref) {
      temp = temp->next;
    }    
    temp->next = newNode;
    newNode->next = *head_ref;
  }
}

//Delete first node by key
void pop_first(struct Node** head_ref, int key) {
  if(*head_ref != NULL) {
    struct Node* temp = *head_ref;
    struct Node* nodeToDelete = *head_ref;

    if(temp->data == key) {
      if(temp->next == *head_ref) {
        *head_ref = NULL;
      } else {

        while(temp->next != *head_ref) {
          temp = temp->next;
        }
        *head_ref = (*head_ref)->next;
        temp->next = *head_ref; 
        free(nodeToDelete); 
      }
    } else {
      
      while(temp->next != *head_ref) {
        if(temp->next->data == key) {
          nodeToDelete = temp->next;
          temp->next = temp->next->next;
          free(nodeToDelete);
          break; 
        }
        temp = temp->next;
      }
    }
  }
}

//display the content of the list
void PrintList(struct Node* head_ref) {
  struct Node* temp = head_ref;
  if(head_ref != NULL) {
    printf("The list contains: ");
    while (1) {
      printf("%i ",temp->data);
      temp = temp->next;
      if(temp == head_ref)
        break;    
    }
    printf("\n");
  } else {
    printf("The list is empty.\n");
  }   
}

// test the code 
int main() {
  struct Node* MyList = NULL;

  //Add five elements in the list.
  push_back(&MyList, 10);
  push_back(&MyList, 20);
  push_back(&MyList, 30);
  push_back(&MyList, 10);
  push_back(&MyList, 20);
  PrintList(MyList);

  //Delete first occurrence of 20
  pop_first(&MyList, 20);
  PrintList(MyList);

  return 0; 
}

The above code will give the following output:

The list contains: 10 20 30 10 20 
The list contains: 10 30 10 20