Insert a node into a Circular Singly linked list
23-09-2019    181 times

### Insert a node in a circular link list

We can insert a new node in a circular link list. We may insert the node in three different location of the node.

1. Beginning of the list
2. End of the list
3. At any said position of the list except first and last location

### Insert into Beginning of the list

In that case, a new node insert at the first position of an existing list. That is, first node or head of the list will be reset after insertion of the node.

#### Algorithms:

Step 1 : Create a new node.

Step 2 : Travel to the last node of the existing node.

Step 3 : Set the next reference of the new node to the first node of the list.

Step 4 : Set the next reference of the last node to the new node of the list.

Step 5 : Reset the first node.

#### Program:

node *insert_first(node *l){
node *l1;
node *temp;
l1=l;
while(l1->next!=l)
{
l1=l1->next;
}
if(l!=NULL){
temp=(node *)malloc(sizeof(node));
printf("Enter a data : ");
scanf("%d",&temp->i);
temp->next=l;
l1->next=temp;
return temp;
}
else
{
return create(l);
}
}


### Insert into Last of the list

In that case, a new node insert at the last position of an existing list. That is, first node or head of the list will not be reset after insertion of the node.

#### Algorithms:

Step 1 : Create a new node.

Step 2 : Travel to the last node of the existing node.

Step 3 : Set the next reference of the new node to the next reference of the last node of the list.

Step 4 : Set the next reference of the last node to the new node of the list.

#### Program:

void insert_end(node *l){
node *temp;
node *l1;
l1=l;
if(l!=NULL){
temp=(node *)malloc(sizeof(node));
printf("Enter a data : ");
scanf("%d",&temp->i);

while(l1->next!=l)
{
l1=l1->next;
}
l1->next=temp;
temp->next=l;
}
}


### In that case, a new node insert at the in between position of an existing list. That is, we can insert a new node at any position of the list except first and last location. Here first node of the list will not be reset after insertion of the node.

#### Algorithms:

Step 1 : Create a new node.

Step 2 : Read the position of the list where we want to insert the new node.

Step 3 : Travel to the node said position-1.

Step 3 : Set the next reference of the new node to the next reference of the current node of the list.

Step 4 : Set the next reference of the current node to the new node of the list.

#### Program:

void insert_pos(node *l,int pos){
node *temp;
int i;
if(l!=NULL){

temp=(node *)malloc(sizeof(node));
printf("Enter a data : ");
scanf("%d",&temp->i);
for(i=1;i<=pos-2;i++)
{
l=l->next;
}
temp->next=l->next;
l->next=temp;
}
}


### This functions call from the main function of program

#include<stdio.h>
#include<conio.h>
{
int i;
};

//prototype declaration
node *insert_first(node *);
void insert_end(node *);
void insert_pos1(node *,int);
int main()
{
int c,i,pos;
while(1)
{
printf("4. Insert into begining\n");
printf("5. Insert into end position\n");
printf("6. Insert into position 1\n");
printf("15. Exit\n");
scanf("%d",&c);
switch(c)
{
case 4:
break;
case 5:
break;
case 6:
printf("Enter position : ");
scanf("%d",&pos);
break;
}
}
}