C Program for Deletion from beginning in Circular Linked List

C Program for Deletion from beginning in Circular Linked List -1

Deleting node from beginning in circular linked list

In this section we will learn C Program for Deletion from beginning in Circular Linked List, here removing first node from the linked list using the pointer to reach the last node of the list, then the last node of the list points to the head node of the list, then this will be changed the last node of the list will point to the second node of the linked list.

Deletion From Beginning in Circular Linked List in C

Algorithm

  • Step 1:   IF HEAD = NULL ( GO TO STEP 8 )
  • Step 2:   NOW SET POINTER = HEAD
  • Step 3:   REPEAT STEP 4 UNTIL POINTER → NEXT != HEAD
  • Step 4:   ASSIGN POINTER = POINTER → NEXT (END OF LOOP)
  • Step 5:   ASSIGN POINTER → NEXT = HEAD → NEXT
  • Step 6:   FREE HEAD NODE
  • Step 7:   ASSIGN HEAD = POINTER → NEXT
  • Step 8:   EXIT

Construction of node in circular linked list :-

struct node
 {
     int data; 
     struct node* next;  
 };
C Program for Deletion from beginning in Circular Linked List

Implementation needed for deleting node at the beginning in the linked list

  • CASE 1 :- If the linked list is empty then head == NULL and exit.
  • CASE 2 :- If the linked  list have only single node then, head → next == head. In this scenario, we need to delete the list and make the head pointer free.
  • CASE 3 :- If the linked list contains more than one node then,  we need to move whole linked list by using the pointer to reach the last node of the list.
  • After that the pointer will point to the last node of the list. So, the last node of the list points to the head node of the list.
  • Therefore we need to change it then, the last node of the list will point to the second node of the linked list.
  • Now free the head node.
  • At last make the node pointed by the next of the last node to the new head of the list.

C Program For Deletion From Beginning in Circular Linked List

Run
#include<stdio.h>
#include<stdlib.h>
// structure for Circular Linked List
struct Node
{
  int data;
  struct Node *next;
};

void deleteBegin(struct Node **head)
{

  struct Node *tempNode = *head;

  // if there are no nodes in Linked List can't delete
  if (*head == NULL)
    {
      printf ("Linked List Empty, nothing to delete");
      return;
    }

  // if only 1 node in CLL
  if (tempNode->next == *head)
    {
      *head = NULL;
      return;
    }

  struct Node *curr = *head;

  // traverse till last node in CLL
  while (curr->next != *head)
    curr = curr->next;

  // assign last node's next to 2nd node in CLL
  curr->next = (*head)->next;

  // move head to next node
  *head = (*head)->next;
  free (tempNode);
}

void insert (struct Node **head, int data)
{
  struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
  newNode->data = data;

  if (*head == NULL)
    {
      *head = newNode;
      (*head)->next = *head;
      return;
    }

  struct Node *curr = *head;

  while (curr->next != *head)
    {
      curr = curr->next;
    }

  curr->next = newNode;
  newNode->next = *head;
  *head = newNode;
}


void display (struct Node *head)
{
  // if there are no node in CLL
  if (head == NULL)
    return;

  struct Node *temp = head;

  //need to take care of circular structure of CLL
  do
    {
      printf ("%d ", temp->data);
      temp = temp->next;

    }
  while (temp != head);
  printf ("\n");
}

int main ()
{

  // first node will be null at creation    
  struct Node *head = NULL;

  insert (&head, 10);
  insert (&head, 11);
  insert (&head, 12);
  insert (&head, 13);
  insert (&head, 14);
  insert (&head, 15);
  insert (&head, 16);

  display (head);

  deleteBegin(&head);
  display (head);

  return 0;
}
Output
16 15 14 13 12 11 10
15 14 13 12 11 10 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Circular Linked List

  • Introduction to Circular Linked List
    Click Here
  • Circular Linked List Applications
    Click Here
  • Circular Linked List in –
    C | C++ | Java
  • Insertion in Circular Linked List –
    C | C++ | Java
  • Insertion at the beginning–
    C | C++ | Java
  • Insertion at the end –
    C | C++ | Java
  • Insertion at nth position –
    C | C++ | Java
  • Deletion in Circular Linked List –
    C | C++ | Java
  • Deletion from beginning in Circular Linked List –
    C | C++ | Java
  • Deletion from nth position in Circular Linked List –
  • Deletion from end in Circular Linked List –
    C | C++ | Java
  • Insertion and Deletion in Circular Linked List – C | C++ | Java
  • Split a Circular Linked List in two halves –
    C | C++ | Java
  • Count nodes in Circular Linked List –
    C | C++ | Java
  • Sorted Insert In Circular Linked List –
    C | C++ | Java
  • Insertion in the middle in Circular Linked List –
    C | C++ | Java

Circular Linked List