Insertion at the end in circular linked list (original) (raw)
Last Updated : 08 Aug, 2024
A circular linked list is a data structure where each node points to the next, and the last node connects back to the first, creating a loop. **Insertion at the end in circular linked list is an important operation. Understanding how to perform this insertion is essential for effectively manage and use circular linked lists in various applications.
Insertion at the end in circular linked list
To insert a new node at the end of a circular linked list, we first create the new node and allocate memory for it. If the list is empty (mean, **last or **tail pointer being NULL), we initialize the list with the **new node and making it point to itself to form a circular structure. If the list already contains nodes then we set the **new node’s next pointer to point to the **current head (which is **tail->next), then update the **current tail's next pointer to point to the **new node. Finally, we update the **tail pointer to the **new node. This will ensure that the **new node is now the **last node in the list while maintaining the circular linkage.
Insertion at the end in circular linked list
Step-by-step approach:
- Create a new node with the given value.
- Check Empty List, If **last is **nullptr then initialize the list with the new node and make it point to itself.
- Otherwise, Set the **new node's next to the **head node.
- Update the current **last's next to point to the **new node.
- Update **last to the new node.
Below is the implementation of the above approach:
C++ `
#include using namespace std;
struct Node{ int data; Node *next; Node(int value) { data = value; next = nullptr; } };
// Function to insert a node at the end of a circular linked list Node *insertEnd(Node *tail, int value) { Node *newNode = new Node(value); if (tail == nullptr){ // If the list is empty, initialize it with the new node tail = newNode;
// Point to itself to form a circular structure
newNode->next = newNode;
}
else{
// Insert new node after the current tail
// and update the tail pointer.
// New node points to the head node
newNode->next = tail->next;
// Tail node points to the new node
tail->next = newNode;
// Update tail to be the new node
tail = newNode;
}
return tail;
}
void printList(Node *last){ if(last == NULL) return;
// Start from the head node
Node *head = last->next;
while (true){
cout << head->data << " ";
head = head->next;
if (head == last->next)
break;
}
cout << endl;
}
int main(){ // Create circular linked list: 2, 3, 4 Node *first = new Node(2); first->next = new Node(3); first->next->next = new Node(4);
Node *last = first->next->next;
last->next = first;
cout << "Original list: ";
printList(last);
// Insert elements at the end of the circular linked list
last = insertEnd(last, 5);
last = insertEnd(last, 6);
cout << "List after inserting 5 and 6: ";
printList(last);
return 0;
}
C
#include <stdio.h> #include <stdlib.h>
// Define the Node structure struct Node { int data; struct Node *next; };
// Function to create a new node struct Node *createNode(int value);
// Function to insert a node at the end of a circular linked list struct Node *insertEnd(struct Node *tail, int value) { struct Node *newNode = createNode(value); if (tail == NULL) { // If the list is empty, initialize it with the new node tail = newNode; newNode->next = newNode; } else { // Insert new node after the current tail and update the tail pointer newNode->next = tail->next; tail->next = newNode; tail = newNode; } return tail; }
// Function to print the circular linked list void printList(struct Node *last) { if (last == NULL) return;
struct Node *head = last->next;
while (1)
{
printf("%d ", head->data);
head = head->next;
if (head == last->next)
break;
}
printf("\n");
}
struct Node *createNode(int value) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; return newNode; }
int main() { // Create circular linked list: 2, 3, 4 struct Node *first = createNode(2); first->next = createNode(3); first->next->next = createNode(4);
struct Node *last = first->next->next;
last->next = first;
printf("Original list: ");
printList(last);
// Insert elements at the end of the circular linked list
last = insertEnd(last, 5);
last = insertEnd(last, 6);
printf("List after inserting 5 and 6: ");
printList(last);
return 0;
}
Java
class Node { int data; Node next;
Node(int value){
data = value;
next = null;
}
}
public class GFG {
// Function to insert a node at the end of a circular
// linked list
static Node insertEnd(Node tail, int value){
Node newNode = new Node(value);
if (tail == null) {
// If the list is empty, initialize it with the
// new node
tail = newNode;
newNode.next = newNode;
}
else {
// Insert new node after the current tail and
// update the tail pointer
newNode.next = tail.next;
tail.next = newNode;
tail = newNode;
}
return tail;
}
// Function to print the circular linked list
static void printList(Node last){
if (last == null) return;
Node head = last.next;
while (true) {
System.out.print(head.data + " ");
head = head.next;
if (head == last.next) break;
}
System.out.println();
}
public static void main(String[] args){
// Create circular linked list: 2, 3, 4
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
System.out.print("Original list: ");
printList(last);
// Insert elements at the end of the circular linked
// list
last = insertEnd(last, 5);
last = insertEnd(last, 6);
System.out.print("List after inserting 5 and 6: ");
printList(last);
}
}
Python
class Node: def init(self, value): self.data = value self.next = None
Function to insert a node at the end of a circular linked list
def insert_end(tail, value): new_node = Node(value) if tail is None: # If the list is empty, initialize # it with the new node tail = new_node new_node.next = new_node else: # Insert new node after the current tail # and update the tail pointer new_node.next = tail.next tail.next = new_node tail = new_node return tail
Function to print the circular linked list
def print_list(last): if last is None: return
head = last.next
while True:
print(head.data, end=" ")
head = head.next
if head == last.next:
break
print()
if name == "main": # Create circular linked list: 2, 3, 4 first = Node(2) first.next = Node(3) first.next.next = Node(4)
last = first.next.next
last.next = first
print("Original list: ", end="")
print_list(last)
# Insert elements at the end of the circular linked list
last = insert_end(last, 5)
last = insert_end(last, 6)
print("List after inserting 5 and 6: ", end="")
print_list(last)
JavaScript
class Node { constructor(value){ this.data = value; this.next = null; } }
// Function to insert a node at the end of a circular linked // list function insertEnd(tail, value){ let newNode = new Node(value); if (tail === null) { // If the list is empty, initialize it with the new // node tail = newNode; newNode.next = newNode; } else { // Insert new node after the current tail and update // the tail pointer newNode.next = tail.next; tail.next = newNode; tail = newNode; } return tail; }
// Function to print the circular linked list function printList(last) { if (last === null) return;
let head = last.next;
while (true) {
console.log(head.data + " ");
head = head.next;
if (head === last.next)
break;
}
console.log();
}
// Create circular linked list: 2, 3, 4 let first = new Node(2); first.next = new Node(3); first.next.next = new Node(4);
let last = first.next.next; last.next = first;
console.log("Original list: "); printList(last);
// Insert elements at the end of the circular linked // list last = insertEnd(last, 5); last = insertEnd(last, 6);
console.log("List after inserting 5 and 6: "); printList(last);
`
Output
Original list: 2 3 4 List after inserting 5 and 6: 2 3 4 5 6
**Time Complexity: O(1)
**Auxiliary Space: O(1)