Table of Contents
Insertion after a Specified Node in Circular Linked List
In this tutorial we will learn about how can we insert a new node after a specified node in circular linked list. We can also understand this as inserting a new node after a given node.
To insert a new node after a specified node in Circular Linked List we use the following steps
Step1 – Create a New Node.
Step 2 – Allocate memory to newly Created Node.
Step 3 – Assign data field value to newnode.
Step 4 – Enter the data value of the specified node after which you want to insert the newnode.
Step 5 – Define two node pointer temp1 and temp2 and initialize with them with head and move temp1 and temp2 until the temp2 represents that node after which you have to insert new node and temp1 refers to node next to temp2.
Step 6 – Set the next pointer of temp2 as newnode and next pointer of newnode with temp1.
Pseudo Code or Function to insert new node after a given node in Circular Linked List is given below
void Insertafter(int num1, int num)
{
struct node *newnode, *temp1, * temp2;
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = num; //Links the data part
newnode->next = NULL;
temp1 = head;
temp2 = head;
/*traverse the linkedlist till the temp2 refer to the node after which you want to insert new // node*/
do
{
temp2=temp1;
temp1 = temp1->next;
}while(temp2->data!=num1);
temp2->next = newnode;
newnode->next=temp1 ;
}
C Program for Insertion after a Specified Node in Circular Linked List
C Program to insert a new node after a specified node or after a given node is explained here in this section.
Let’s understand the program.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * next;
};
struct node *head,*firstnode,*lastnode;
void createList(int n);
void displayList();
void Insertafter(int num1,int num);
int main()
{
int n,value1, value;
printf(“Enter the total number of nodes: “);
scanf(“%d”, &n);
createList(n); // calling Function inside main()
printf(“\n Linked List is \n”);
displayList(); // calling function inside main()
printf(“\nenter the data value of node after which you want to insert new node”);
scanf(“%d”, & value1);
printf(“\nenter value of new node to be inserted “);
scanf(“%d”, & value);
Insertafter(value1 , value);// calling function
printf(“\nAfter Inserting new node after a given node the listis \n”);
displayList(); // calling function inside main()
return 0;
}
void createList(int n)
{
int i, data;
struct node *newNode;
firstnode = (struct node *)malloc(sizeof(struct node));
head=firstnode;
printf(“Enter data of 1 node: “);
scanf(“%d”, &data);
firstnode->data = data;
firstnode->next = NULL;
lastnode = firstnode;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node)) ;
printf(“Enter data of %d node: “, i);
scanf(“%d”, &data);
newNode->data = data;
newNode->next = NULL;
// Link the previous node with newly created node
lastnode->next = newNode;
// Move the previous node ahead
lastnode = newNode;
}
// when Linked List is created with n nodes then to make it circular Link // the last node with first node
lastnode->next=head;
printf(“\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n”);
}
/**
* Display the content of the list
*/
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf(“List is empty.\n”);
}
else
{
temp = head;
printf(“Linked List is\n”);
do {
printf(“%d\t”, temp ->data);
temp = temp->next;
}while(temp!= head);
}
}
void Insertafter(int num1, int num)
{
struct node *newnode, *temp1, * temp2;
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = num; //Links the data part
newnode->next = NULL;
temp1 = head;
temp2 = head;
do
{
temp2=temp1;
temp1 = temp1->next;
}while(temp2->data!=num1);
temp2->next = newnode;
newnode->next=temp1 ;
}
OUTPUT
Enter the total number of nodes: 4
Enter data of 1 node: 21
Enter data of 2 node: 22
2Enter data of 3 node: 23
Enter data of 4 node: 24
CIRCULAR LINKED LIST CREATED SUCCESSFULLY
Linked List is
Linked List is
21 22 23 24
enter the data value of node after which you want to insert new node
23
enter value of new node to be inserted 55
After Inserting new node after a given node the listis
Linked List is
21 22 23 55 24
Conclusion and Summary
- In this tutorial we have explained the algorithm to insert a new node after a given node in circular linked list.
- C Program for the insertion after a specified node in circular linked list is also discussed.