VOOZH about

URL: https://www.geeksforgeeks.org/dsa/insertion-at-specific-position-in-a-circular-doubly-linked-list/

⇱ Insertion at Specific Position in a Circular Doubly Linked List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Insertion at Specific Position in a Circular Doubly Linked List

Last Updated : 11 Jul, 2025

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. 

👁 Image

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:  

  1. Create a newNode in the memory.
  2. Traverse in the list using a temporary pointer(temp) till the node just before the given position at which a new node is needed to be inserted.
  3. Insert the new node by performing below operations: 
    • Assign newNode->next = temp->next
    • Assign newNode->prev as temp->next
    • Assign temp->next as newNode
    • Assign (temp->next)->prev as newNode->next

Below is the implementation of the above idea:  


Output
The list is: 1 2 3 4 5 6
The list is: 1 2 8 3 4 5 6

complexities Analysis:

  • Time Complexity:O(n) => for counting the list as we are using a loop to traverse linearly, O(n) => Inserting the elements, as we are using a loop to traverse linearly. So, total complexity is O(n + n) = O(n). Where n is the number of nodes in the linked list.
  • Auxiliary Space: O(1), as we are not using any extra space. 

New Approach:- Here's an alternative approach to inserting an element at a specific position in a circular doubly linked list.

Algorithm :

1. Define the structure for a doubly linked list node (`Node`) with data, `next` pointer, and `prev` pointer.

2. Create a function `getNode` that allocates memory for a new node, initializes its data and pointers, and returns the new node.

3. Create a function `displayList` to print the elements of the circular doubly linked list. It traverses the list starting from the `start` node and prints the data of each node until it reaches the `start` node again.

4. Create a function `countList` to count the number of elements in the circular doubly linked list. It starts from the `start` node and increments a counter while traversing the list until it reaches the `start` node again. The final count is returned.

5. Create a function `insertAtLocation` to insert a new node at a given position in the circular doubly linked list. It takes the address of the `start` pointer, the data to be inserted, and the desired position as input.

6. First, count the number of elements in the list using the `countList` function. If the specified position is less than 1 or greater than the count plus one, return false to indicate an invalid position.

7. Create a new node using `getNode` function and assign the input data to it.

8. If the list is empty (start pointer is NULL), make the new node the start node by pointing its `next` and `prev` pointers to itself.

9. If the desired position is 1, insert the new node at the beginning of the list. Update the pointers of the new node, the previous start node, and the last node in the list to maintain the circular doubly linked structure.

10. If the desired position is other than 1, traverse the list until the node just before the desired position. Update the pointers of the new node, the current node, and the next node to insert the new node at the desired position.

11. Finally, return true to indicate successful insertion.

12. In the `main` function, create the circular doubly linked list by inserting elements from the given array using the `insertAtLocation` function.

13. Display the list before insertion.

14. Insert a new node with data 8 at the 3rd position using the `insertAtLocation` function.

15. Display the list after insertion.

16. The program ends.

Below is the implementation of the above idea:  

Output:-

The list is: 1 2 3 4 5 6 
The list is: 1 2 8 3 4 5 6

The time complexity :

1. `getNode` function: O(1) - It takes constant time to create a new node.

2. `displayList` function: O(n) - It traverses the entire circular doubly linked list to display its elements. Since there are n elements in the list, the time complexity is O(n).

3. `countList` function: O(n) - It also traverses the entire circular doubly linked list to count the number of elements. Therefore, the time complexity is O(n).

4. `insertAtLocation` function:
  - If the location is valid and not at the beginning: O(loc) - It traverses to the node before the desired position, which takes at most loc-1 iterations.
  - If the location is at the beginning: O(1) - It performs constant time operations to insert the new node at the beginning.
  - Counting the number of elements in the list: O(n) - It calls the `countList` function, which has a time complexity of O(n).

Overall, the time complexity of the `insertAtLocation` function is O(max(loc, n)) since it depends on the larger value between loc and the number of elements in the list.

5. `main` function:
  - Creating the circular doubly linked list: O(n) - It inserts n elements into the list using the `insertAtLocation` function, which has a time complexity of O(max(loc, n)).
  - Displaying the list: O(n) - It calls the `displayList` function, which has a time complexity of O(n).
  - Inserting 8 at the 3rd position: O(max(loc, n)) - It calls the `insertAtLocation` function, which has a time complexity of O(max(loc, n)).
  - Displaying the updated list: O(n) - It calls the `displayList` function, which has a time complexity of O(n).

Therefore, the overall time complexity of the `main` function is O(n + max(loc, n) + n) = O(max(loc, n)).

The auxiliary space complexity :- of the code is O(1) since it uses a fixed amount of additional memory regardless of the input size.

Comment