Insertion at Specific Position in a Circular Doubly Linked List
Prerequisite:
Given the start pointer pointing to the start of a Circular Doubly Linked List, an element and a position. The task is to insert the element at the specified position in the given Circular Doubly Linked List.

The idea is to count the total number of elements in the list. Check whether the specified location is valid or not, i.e. location is within the count.
If location is valid:
- Create a newNode in the memory.
- Traverse in the list using a temporary pointer(temp) till node just before the given position at which new node is needed to be inserted.
- Insert the new node by performing below operations:
- Assign newNode->next = temp->next
- Assign newNode->prev as temp->next
- Assign temp->next as newNode
- Assgin (temp->next)->prev as newNode->next
Below is the implementation of the above idea:
C++
// CPP program to convert insert an element at a specific // position in a circular doubly linked list #include <iostream> using namespace std; // Doubly linked list node struct node { int data; struct node* next; struct node* prev; }; // Utility function to create a node in memory struct node* getNode() { return ((struct node*)malloc(sizeof(struct node))); } // Function to display the list int displayList(struct node* temp) { struct node* t = temp; if (temp == NULL) return 0; else { cout << "The list is: "; while (temp->next != t) { cout << temp->data << " "; temp = temp->next; } cout << temp->data << endl; return 1; } } // Function to count nunmber of // elements in the list int countList(struct node* start) { // Decalre temp pointer to // traverse the list struct node* temp = start; // Variable to store the count int count = 0; // Iterate the list and increment the count while (temp->next != start) { temp = temp->next; count++; } // As the list is circular, increment the // counter at last count++; return count; } // Function to insert a node at a given position // in the circular doubly linked list bool insertAtLocation(struct node* start, int data, int loc) { // Declare two pointers struct node *temp, *newNode; int i, count; // Create a new node in memory newNode = getNode(); // Point temp to start temp = start; // count of total elements in the list count = countList(start); // If list is empty or the position is // not valid, return false if (temp == NULL || count < loc) return false; else { // Assign the data newNode->data = data; // Iterate till the loc for (i = 1; i < loc - 1; i++) { temp = temp->next; } // See in Image, circle 1 newNode->next = temp->next; // See in Image, Circle 2 (temp->next)->prev = newNode; // See in Image, Circle 3 temp->next = newNode; // See in Image, Circle 4 newNode->prev = temp; return true; } return false; } // Function to create circular doubly linked list // from array elements void createList(int arr[], int n, struct node** start) { // Declare newNode and temporary pointer struct node *newNode, *temp; int i; // Iterate the loop until array length for (i = 0; i < n; i++) { // Create new node newNode = getNode(); // Assign the array data newNode->data = arr[i]; // If it is first element // Put that node prev and next as start // as it is circular if (i == 0) { *start = newNode; newNode->prev = *start; newNode->next = *start; } else { // Find the last node temp = (*start)->prev; // Add the last node to make them // in circular fashion temp->next = newNode; newNode->next = *start; newNode->prev = temp; temp = *start; temp->prev = newNode; } } } // Driver Code int main() { // Array elements to create // circular doubly linked list int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = sizeof(arr) / sizeof(arr[0]); // Start Pointer struct node* start = NULL; // Create the List createList(arr, n, &start;); // Display the list before insertion displayList(start); // Inserting 8 at 3rd position insertAtLocation(start, 8, 3); // Display the list after insertion displayList(start); return 0; } |
Java
// Java program to convert insert // an element at a specific position // in a circular doubly linked listing, // end and middle class GFG { // Doubly linked list node static class node { int data; node next; node prev; }; // Utility function to create a node in memory static node getNode() { return new node(); } // Function to display the list static int displayList( node temp) { node t = temp; if (temp == null) return 0; else { System.out.println( "The list is: "); while (temp.next != t) { System.out.print( temp.data + " "); temp = temp.next; } System.out.println( temp.data ); return 1; } } // Function to count nunmber of // elements in the list static int countList( node start) { // Decalre temp pointer to // traverse the list node temp = start; // Variable to store the count int count = 0; // Iterate the list and // increment the count while (temp.next != start) { temp = temp.next; count++; } // As the list is circular, increment // the counter at last count++; return count; } // Function to insert a node at // a given position in the // circular doubly linked list static node insertAtLocation( node start, int data, int loc) { // Declare two pointers node temp, newNode; int i, count; // Create a new node in memory newNode = getNode(); // Point temp to start temp = start; // count of total elements in the list count = countList(start); // If list is empty or the position is // not valid, return false if (temp == null || count < loc) return start; else { // Assign the data newNode.data = data; // Iterate till the loc for (i = 1; i < loc - 1; i++) { temp = temp.next; } // See in Image, circle 1 newNode.next = temp.next; // See in Image, Circle 2 (temp.next).prev = newNode; // See in Image, Circle 3 temp.next = newNode; // See in Image, Circle 4 newNode.prev = temp; return start; } } // Function to create circular doubly // linked list from array elements static node createList(int arr[], int n, node start) { // Declare newNode and temporary pointer node newNode, temp; int i; // Iterate the loop until array length for (i = 0; i < n; i++) { // Create new node newNode = getNode(); // Assign the array data newNode.data = arr[i]; // If it is first element // Put that node prev and next as start // as it is circular if (i == 0) { start = newNode; newNode.prev = start; newNode.next = start; } else { // Find the last node temp = (start).prev; // Add the last node to make them // in circular fashion temp.next = newNode; newNode.next = start; newNode.prev = temp; temp = start; temp.prev = newNode; } } return start; } // Driver Code public static void main(String args[]) { // Array elements to create // circular doubly linked list int arr[] = { 1, 2, 3, 4, 5, 6 }; int n = arr.length; // Start Pointer node start = null; // Create the List start = createList(arr, n, start); // Display the list before insertion displayList(start); // Inserting 8 at 3rd position start = insertAtLocation(start, 8, 3); // Display the list after insertion displayList(start); } } // This code is contributed by Arnab Kundu |
C#
// C# program to convert insert // an element at a specific position // in a circular doubly linked listing, // end and middle using System; class GFG { // Doubly linked list node public class node { public int data; public node next; public node prev; }; // Utility function to create a node in memory static node getNode() { return new node(); } // Function to display the list static int displayList( node temp) { node t = temp; if (temp == null) return 0; else { Console.WriteLine( "The list is: "); while (temp.next != t) { Console.Write( temp.data + " "); temp = temp.next; } Console.WriteLine( temp.data ); return 1; } } // Function to count nunmber of // elements in the list static int countList( node start) { // Decalre temp pointer to // traverse the list node temp = start; // Variable to store the count int count = 0; // Iterate the list and // increment the count while (temp.next != start) { temp = temp.next; count++; } // As the list is circular, increment // the counter at last count++; return count; } // Function to insert a node at // a given position in the // circular doubly linked list static node insertAtLocation( node start, int data, int loc) { // Declare two pointers node temp, newNode; int i, count; // Create a new node in memory newNode = getNode(); // Point temp to start temp = start; // count of total elements in the list count = countList(start); // If list is empty or the position is // not valid, return false if (temp == null || count < loc) return start; else { // Assign the data newNode.data = data; // Iterate till the loc for (i = 1; i < loc - 1; i++) { temp = temp.next; } // See in Image, circle 1 newNode.next = temp.next; // See in Image, Circle 2 (temp.next).prev = newNode; // See in Image, Circle 3 temp.next = newNode; // See in Image, Circle 4 newNode.prev = temp; return start; } } // Function to create circular doubly // linked list from array elements static node createList(int []arr, int n, node start) { // Declare newNode and temporary pointer node newNode, temp; int i; // Iterate the loop until array length for (i = 0; i < n; i++) { // Create new node newNode = getNode(); // Assign the array data newNode.data = arr[i]; // If it is first element // Put that node prev and next as start // as it is circular if (i == 0) { start = newNode; newNode.prev = start; newNode.next = start; } else { // Find the last node temp = (start).prev; // Add the last node to make them // in circular fashion temp.next = newNode; newNode.next = start; newNode.prev = temp; temp = start; temp.prev = newNode; } } return start; } // Driver Code public static void Main() { // Array elements to create // circular doubly linked list int []arr = { 1, 2, 3, 4, 5, 6 }; int n = arr.Length; // Start Pointer node start = null; // Create the List start = createList(arr, n, start); // Display the list before insertion displayList(start); // Inserting 8 at 3rd position start = insertAtLocation(start, 8, 3); // Display the list after insertion displayList(start); } } /* This code contributed by PrinciRaj1992 */ |
The list is: 1 2 3 4 5 6 The list is: 1 2 8 3 4 5 6
Time Complexity: O(n) => for counting the list, O(n) => Inserting the elements. So, total complexity is O(n + n) = O(n)
Recommended Posts:
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Doubly Linked List | Set 1 (Introduction and Insertion)
- Insertion Sort for Doubly Linked List
- Insert a node at a specific position in a linked list
- Circular Singly Linked List | Insertion
- Doubly Circular Linked List | Set 2 (Deletion)
- Reverse a doubly circular linked list
- Insertion in a sorted circular linked list when a random pointer is given
- Search an Element in Doubly Circular Linked List
- Convert an Array to a Circular Doubly Linked List
- Delete a Doubly Linked List node at a given position
- Convert a given Binary Tree to Circular Doubly Linked List | Set 2
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Convert a Binary Tree to a Circular Doubly Link List
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



