C Program for Insertion at nth node in circular linked list

Insertion at nth position in Circular Linked List in C

Inserting a new node at the nth node in circular linked list

Today we will learn how to write a code in C Program for Insertion at nth node in circular linked list where the user can add new node at any position in the linked list.Circular linked list is the advance version of singly and doubly linked list.
For Example :- Input : 8 5 2 11 9
Where you want to insert new node at position : 2
Enter the data you want to insert at position 2 : 40
Output :- 8 40 5 2 11 9

Algorithm for inserting node at the specific position of the circular linked list :-

  • Step 1: ASSIGN NEW NODE AND CURRENT NODE
  • Step 2: IF HEAD == NULL ( GOTO STEP 9 )
  • Step 3: ELSE NEW NODE → DATA = DATA
  • Step 4: CURRENT NODE = HEAD
  • Step 5: FOR I=2 TO POS-1
  • Step 6: CURRENT NODE=CURRENT NODE → NEXT (END OF FOR LOOP)
  • Step 7: NEW NODE → NEXT=CURRENT NODE → NEXT
  • Step 8: CURRENT NODE → NEXT = NEW NODE
  • Step 9: EXIT

Working require for putting node at the nth node of the circular linked list :-

  • Make a new node and set the data.
  • Move to pos-1 position in the circular linked list.
  • Now link the next pointer of new node with the node pointed by the next pointer of current(pos-1) node.
  • After that join the next pointer of current node with the newly created node which means that the next pointer of current node will point to new node.
  • At last print the  linked list.

Structure of node in circular linked list :-

Code :-

     struct node
 {
     int data; 
     struct node* next;  
 };
C program for insertion at nth node in Circular linked list
C Program for insertion at in cirular linked list
#include <stdio.h> 
#include <stdlib.h> 
struct node    //structure of node in circular
{
  int data;
  struct node *nextpt;
} *headpt;

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 insertion_At_begin (int data)	// insertion function used for node to put in beginning of circular linked list
{
  struct node *new_node, *cur_Node;
  if (headpt == NULL)
    {
      printf ("The List is empty");
    }
  else
    {
      new_node = (struct node *) malloc (sizeof (struct node));
      new_node->data = data;
      new_node->nextpt = headpt;
      cur_Node = headpt;
      while (cur_Node->nextpt != headpt)
	{
	  cur_Node = cur_Node->nextpt;
	}
      cur_Node->nextpt = new_node;
      headpt = new_node;
    }
}

void insert_At_Any_Node (int data, int pos) //function for insertion at user required position
{
  struct node *new_node, *cur_Node;
  int i;

  if (headpt == NULL)
    {
      printf ("List is empty");
    }
  else if (pos == 1)
    {
      insertion_At_begin (data);
    }
  else
    {
      new_node = (struct node *) malloc (sizeof (struct node));
      new_node->data = data;
      cur_Node = headpt;
      for (i = 2; i nextpt;
	}
      new_node->nextpt = cur_Node->nextpt;
      cur_Node->nextpt = new_node;
    }
}
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 circular list is :\n");
   }
  do
  {
   printf ("The data at node %d :: %d\n", n, extra->data);
   extra = extra->nextpt;
   n++;
  }
  while (extra != headpt);
 }
}

int main ()
{
  int n, value, data1, x;
  printf ("Enter the size of nodes: \n");
  scanf ("%d", &n);
  list (n);
  print (x);
  printf ("Enter the position to insert a new node data : ");
  scanf ("%d", &value);
  printf ("Enter the data for new node at position %d : ", value);
  scanf ("%d", &data1);
  insert_At_Any_Node (data1, value);
  print (x);
  return 0;
}
Output :-
Enter the size of nodes: 
5
Enter data for node 1 : 23
Enter data for node 1 : 10
Enter data for node 1 : 5
Enter data for node 1 : 7
Enter data for node 1 : 61
After insertion the circular list is :
The data at node 1 :: 23
The data at node 2 :: 10
The data at node 3 :: 5
The data at node 4 :: 7
The data at node 5 :: 61

Enter the data to be inserted at the end: 90
After insertion the circular list is :
The data at node 1 :: 23
The data at node 2 :: 10
The data at node 3 :: 5
The data at node 4 :: 7
The data at node 5 :: 61
The data at node 6 :: 90