# 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

## 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;
};```

### 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
{
return;
}

// if LL already as >=1 node

// traverse till last node in LL
{
curr = curr->next;
}
// assign LL's last node's next as this new node
curr->next = newNode;

// assign newNode's next as current head

// change head to this new node
}

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
{
return;
}

// if LL already as >=1 node

// traverse till last node in LL
{
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
}

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

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

{
// if there are no node in LL
return;

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

}
printf ("\n");
}

int main ()
{

printf("Insert at beginning: ");

printf("Insert at End: ");

printf("Insert at Specific Position: ");

return 0;
}
```

### Output

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

### 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

• Introduction to Circular Linked List
• 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