C Program for Deletion from beginning in Circular Linked List

Deletion at beginning in Circular Linked List using C

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.

Algorithm for deleting node from starting in the circular linked list :-

  • 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

Syntax :-

     struct node
 {
     int data; 
     struct node* next;  
 };

Construction of node 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
C Program for deletion from in cirular linked list
#include <stdio.h>
#include <stdlib.h>

struct node        //structure of node
{
  int data;
  struct node *nextpt;
} *headpt;

void delete_begin_node ()		//function for deleting first node in the linked list
{
  struct node *n, *save;
  n = headpt;
  while (n->nextpt != headpt)
    {
      n = n->nextpt;
    }
  save = headpt;
  headpt = headpt->nextpt;
  printf ("The deletion at beginning is -> %d", save->data);
  n->nextpt = headpt;
  free (save);
}

void list (int n)			// list creation function of circular linked list
{
  int l, data;
  struct node *prevpt, *new_node;

  if (n >= 1)
    {
      headpt = (struct node *) malloc (sizeof (struct node));

      printf ("Enter data for node 1 : ");
      scanf ("%d", &data);
      headpt->data = data;
      headpt->nextpt = NULL;
      prevpt = headpt;
      for (l = 2; l <= n; l++)
	{
	  new_node = (struct node *) malloc (sizeof (struct node));
	  printf ("Enter data for node %d : ", l);
	  scanf ("%d", &data);
	  new_node->data = data;
	  new_node->nextpt = NULL;
	  prevpt->nextpt = new_node;
	  prevpt = new_node;
	}
      prevpt->nextpt = headpt;
    }
}

void print (int m)			// print function of circular linked list
{
  struct node *extra;
  int n = 1;

  if (headpt == NULL)
    {
      printf ("The List is empty");
    }
  else
    {
      extra = headpt;
      if (m == 1)
	{
	  printf ("The circular linked list is :\n");
	}
      else
	{
	  printf ("After insertion the list is :\n");
	}
      do
	{
	  printf ("The data at node %d is %d\n", n, extra->data);
	  extra = extra->nextpt;
	  n++;
	}
      while (extra != headpt);
    }
}

int main ()
{
  int value, n, x;
  printf ("Enter the size of nodes: \n");
  scanf ("%d", &n);
  list (n);
  print (x);
  delete_begin_node (value);
  return 0;
}
Output :-
Enter the size of nodes: 
5
Enter data for node 1 : 65
Enter data for node 2 : 74
Enter data for node 3 : 12
Enter data for node 4 : 30
Enter data for node 5 : 9
After insertion the list is :
The data at node 1 is 65
The data at node 2 is 74
The data at node 3 is 12
The data at node 4 is 30
The data at node 5 is 9
The deletion at beginning is -> 65

To learn more