- Prepare
All Platforms Programming Aptitude Syllabus Interview Preparation Interview Exp. Off Campus - Prime Video
- Prime Mock

- Interview Experience
- Prime VideoNew
- Prime Mock
- Interview Prep
- Nano Degree
- Prime Video
- Prime Mock

# Insertion in circular linked list in C++

## What is insertion in circular linked in CPP programming?

**Insertion in circular linked in C++ in general words can be understood as insertion of new node in the predefined circular linked list. We can perform three different types of insertion in circular linked list, these different types of insertion are:-**

**Insertion at beginning of the circular linked list.****Insertion at specific position in circular linked list.****Insertion at end in circular linked list.**

**In this article, we will discuss what is the difference between these types of insertion and how to perform them.**

**Types of insertion in circular linked list**

### 1.) Insertion at beginning:

In this type of insertion in circular linked list we add a new node before the head node of the circular linked list. This newly added node will now act as head of the list.

### 2.) Insertion at specific position:

If we want to insert a new node at specific position then we need to traverse till that position and insert a new node there shifting the position of remaining nodes by one.

### 3.) Insertion at end:

In this type of insertion in circular linked list a new node is added after the end node of the list, and this newly added node will now act as tail of the list.

## Insertion at beginning

## Insertion at beginning

## How to perform insertion at beginning in circular linked list?

To perform insertion at beginning in circular linked list we will use the following steps:-

- We will create a circular linked list with some data in it.
- We will define a node pointer and this node pointer as head of the list.
- Now we will traverse this node pointer till the end of the list and follow the algorithm for insertion at beginning in circular linked list below.

void insertBeginning(int num) { struct node *newnode, *curNode; if(head == NULL) { cout<<"List is empty"; } else { newnode = (struct node *)malloc(sizeof(struct node));

newnode->num = num; newnode->next = head; curNode = head; while(curNode->next != head) { curNode = curNode->next; } curNode->next = newnode; head = newnode; } }

## Insertion at specific position

## Insertion at specific position

## How to perform insertion at specific position in circular linked list?

To perform insertion at specific position in circular linked list we will use the following steps:-

- We will create a circular linked list with some data in it.
- Now we will traverse till that specific position using a node pointer.
- Next we will insert the new node after the node pointing by node pointer using the algorithm below

```
void insertSpecificPosition(int num, int pos)
{
struct node *newnode, *curNode;
int i;
if(head == NULL)
{
cout<<"List is empty";
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num;
curNode = head;
for(i=0; i<=pos-3; i++)
{
curNode = curNode->next;
}
newnode->next = curNode->next;
curNode->next = newnode;
}
}
```

## Insertion at end

## Insertion at end

## How to perform insertion at end in circular linked list?

To perform insertion at end in circular linked list we will use the following steps:-

After creating a linked list with some data in it we will define:-

- We will define a node pointer this will initially point the head of the list and later will be used for traversing till the end of the list
- Now traverse till the end of the list with the help of node pointer.
- Now insert new node after the last node using the algorithm below.

```
void insertEnd(int num1)
{
struct node *p;
int a;
a=num1;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->num=a;
p=head;
while(p->next!=head)
{
p=p->next;
}
p->next=temp;
temp->next=head;
}
```

Login/Signup to comment