C Program for Deletion from nth position in circular linked list

Perform deletion at nth node in circular linked list

In this section we will learn how to write a code in C Program for Deletion from nth position in circular linked list. The circular linked list have more features than doubly or singly  linked list. In circular linked list the last node pointer point the first node of the list, hence make a loop.
For Example :- Input : 45 2 3 9 84 2
Enter the position you want to delete node= 2
Output : 45 3 9 84 2

Deletion at nth position in Circular linked list in C

Working for deleting node in the circular linked list at nth position :-

  • Assign the two pointer, name them u and v.
  • Assign a new count variable as w.
  • And Assign delete with position – 1.
  • Then, run for loop while w index not equal to delete.
  • In this for loop make u = v and u = u → next pointer.
  • As the value of w increase one on every successful iteration of loop.
  • Then make the next of v equal to next of u.
  • At last free the node pointed by pointer u.

How to construct node in doubly :-

Syntax :-

struct node   
{  
    struct node *prev;   
    int data;  
    struct node *next;   
}   
C Program for deletion from nth position in Circular Linked List

Algorithm for deleting node at the nth position in the circular linked list :-

  • Step 1: MAKE A TWO NEW NODE U AND V
  • Step 2: SET VALUE OF W = 0
  • Step 3: THEN SET DELETE = POSITION-1
  • Step 4: MAKE U = HEAD
  • Step 5: MAKE A LOOP WHILE (W != DELETE)
  • Step 6: SET VALUE EQUAL TO V = U
  • Step 7: THEN U = U → NEXT
  • Step 8: INCREMENT VARIABLE VALUE W++
  • Step 9: AT LAST V → NEXT = U →NEXT
  • Step 10: FREE NODE U

C Program for Deletion from nth position in circular linked list :-

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

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

void delete_node_at_specific (int value)	//function to delete any node from the list
{
  struct node *u, *v;
  int delete, w = 0;
  delete = value - 1;
  u = headpt;
  while (w != delete)
    {
      v = u;
      u = u->nextpt;
      w++;
    }
  v->nextpt = u->nextpt;
  free (u);			//free node
}

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 that the circular linked 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 : ");
  scanf ("%d", &n);
  list (n);
  print (x);
  printf ("Enter position from where you want to delete the node : ");
  scanf ("%d", &value);
  delete_node_at_specific (value);
  print (x);
  return 0;
}
Output :-
Enter the size of nodes : 6
Enter data for node 1 : 61
Enter data for node 2 : 12
Enter data for node 3 : 9
Enter data for node 4 : 45
Enter data for node 5 : 32
Enter data for node 6 : 6
After that the circular linked list is :
The data at node 1 is 61
The data at node 2 is 12
The data at node 3 is 9
The data at node 4 is 45
The data at node 5 is 32
The data at node 6 is 6
Enter position from where you want to delete the node : 3
After that the circular linked list is :
The data at node 1 is 61
The data at node 2 is 12
The data at node 3 is 45
The data at node 4 is 32
The data at node 5 is 6