×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Create a Circular Singly linked list Forward and Reverse Display of node of a Circular Singly linked list
Insert a node into a Circular Singly linked list - Link list - C Language
1308    Arnab De    23/09/2019

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
softetechnologies

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.

softetechnologies

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

Insert into any said position of the list except first and last location

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;
	}
}
softetechnologies

This functions call from the main function of program

#include<stdio.h>
#include<conio.h>
struct linklist
{
	int i;
	struct linklist *next;
};
typedef struct linklist node;

//prototype declaration
node *insert_first(node *);
void insert_end(node *);
void insert_pos1(node *,int);
node *head;
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");
		printf("Enter Your choice : ");
		scanf("%d",&c);
		switch(c)
		{
			case 4:
				head=insert_first(head); // insert from beg
				break;
			case 5:
				insert_end(head); // insert from end
				break;
			case 6:
				printf("Enter position : ");
				scanf("%d",&pos);
				insert_pos1(head,pos);
				break;
		}	
	}
}
Create a Circular Singly linked list Forward and Reverse Display of node of a Circular Singly linked list
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Albert Einstein
Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whol
Albert Einstein
3640
51.71
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     41743
01/01/2018     35913
28/06/2017     33954
25/06/2018     33202
02/08/2017     32306
06/07/2017     26716
01/08/2017     26706
15/05/2017     26325
14/07/2017     21730
21/04/2018     20622
softetechnologies