C Program for Insertion at nth node in circular linked list

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

C Program for Insertion at End in circular linked list

Algorithm For Inserting Node At Specific Position

  • 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

Structure Of Node

     struct node
 {
     int data; 
     struct node* next;  
 };
Insertion in Circular Linked List in C 2

Working Require For Putting Node At The nth Node

  • 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.

Code For Insertion At Specific Position In Circular Linked List

Run
#include<stdio.h>
#include<stdlib.h>
struct Node
{
  int data;
  struct Node *next;
};
void insertStart (struct Node **head, int data)
{
  struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
  newNode->data = data;

  // if its the first node being entered
  if (*head == NULL)
    {
      *head = newNode;
      (*head)->next = *head;
      return;
    }

  // if LL already as >=1 node
  struct Node *curr = *head;

  // traverse till last node in LL
  while (curr->next != *head)
    {
      curr = curr->next;
    }
  // assign LL's last node's next as this new node
  curr->next = newNode;

  // assign newNode's next as current head
  newNode->next = *head;

  // change head to this new node
  *head = newNode;
}

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

  // if its the first node being entered
  if (*head == NULL)
    {
      *head = newNode;
      (*head)->next = *head;
      return;
    }

  // if LL already as >=1 node
  struct Node *curr = *head;

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

  // assign LL's current last node's next as this new node
  curr->next = newNode;

  // assign this new node's next as current head of LL
  newNode->next = *head;
}

void insertPosition (int data, int pos, struct Node **head)	
//function to insert element at specific position
{
  struct Node *newnode, *curNode;
  int i;

  if (*head == NULL)
    {
      printf ("List is empty");
    }
  if (pos == 1)
    {
      insertStart (head, data);
      return;
    }
  else
    {
      newnode = (struct Node *) malloc (sizeof (struct Node));
      newnode->data = data;
      curNode = *head;
      while (--pos > 1)
	{
	  curNode = curNode->next;
	}
      newnode->next = curNode->next;
      curNode->next = newnode;
    }
}

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

  struct Node *temp = head;

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

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

int main ()
{

  struct Node *head = NULL;
  
  printf("Insert at beginning: ");
  insertStart (&head, 2);
  insertStart (&head, 1);
  display (head);

  printf("Insert at End: ");
  insertLast (&head, 30);
  insertLast (&head, 40);
  display (head);

  printf("Insert at Specific Position: ");
  insertPosition (5, 3, &head);
  display (head);

  return 0;
}

Output

Insert at beginning: 1 2 
Insert at End: 1 2 30 40 
Insert at Specific Position: 1 2 5 30 40

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

One comment on “C Program for Insertion at nth node in circular linked list”


  • chinmay

    The code is wrong 2 error are present in your code position are not allocate in properly