Table of Contents
Insertion at the End of Circular Linked List
- In this tutorial we will learn about the Insertion at the end of Circular Singly Linked List.
- In previous tutorial we have discussed the Insertion in the beginning of circular linked list.
Algorithm or steps to insert a new node at the end of circular linked list is given below –
Define a structure of node
struct node
{
int data ;
struct node *next;
};
Algorithm for Insertion at the End of Circular Linked List
We have to follow the given steps to insert a new node at the end of circular linked list.
Step1 – Create a New Node
Struct node *newnode ;
Step 2 – Allocate memory to newly Created Node.
newnode =(struct node*)malloc(sizeof(struct node));
Step 3 – Assign data field value to newnode.
newnode->data= value;
Step 4. To add new node in the end of circular linked list set the next pointer of lastnode with newnode and set next pointer of newnode with head to connect it with first node of linked list and make the newnode as last node.
newnode->next=firstnode;
lastnode->next = newnode;
lastnode = newnode
Pseudocode or Function to insert newnode at the End of Circular Linked List is given below
void insertAtEnd(int value)
{
struct node* newnode;
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = value;
newnode->next=firstnode;
lastnode->next = newnode;
lastnode = newnode;
}
C Program to insert a new node at the end of circular Singly linked list is given below –
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * next;
};
struct node *head,*firstnode,*lastnode;
// Declare functions to create , display and insert new node
void createList(int n);
void displayList();
void insertAtEnd(int value);
int main()
{
int numnodes, value1;
printf(“enter the number of nodes”);
scanf(“%d”, &numnodes);
createList(numnodes);
displayList();
printf(“enter the data field value of new node to be insert”);
scanf(“%d”,&value1);
insertAtEnd(value1);
printf(“linkedlist after insert newnode at end of list \n”);
displayList();
return 0;
}
/* Function to create a circular linked list with n nodes */
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”);
}
/* Function to Display the content of Circular Singly Linked 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);
}
}
// Pseudocode or Function to insert newnode at the End of Circular Linked List is given //below
void insertAtEnd(int value)
{
struct node* newnode;
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = value;
newnode->next=firstnode;
lastnode->next = newnode;
lastnode = newnode;
}
OUTPUT
enter the number of nodes
4
Enter data of 1 node: 21
2Enter data of 2 node: 2
Enter data of 3 node: 23
Enter data of 4 node: 24
CIRCULAR LINKED LIST CREATED SUCCESSFULLY
Linked List is
21 2 23 24 enter the data field value of new node to be insert
65
linkedlist after insert newnode at end of list
Linked List is
21 2 23 24 65
Conclusion and Summary
- In this tutorial we have discussed and explained the insertion at the end of the circular linked list.
- C Program to insert a node at the end of circular singly linked list is also explained.
I hope the concepts discussed in this tutorial will be useful for computer science students.
Next Tutorial – Insertion after a Given Node in Circular Linked List