Insertion at Specific Position in a Circular Doubly Linked List | CPP program to convert insert an element at a specific position in a circular doubly linked list ; Doubly linked list node ; Utility function to create a node in memory ; Function to display the list ; Function to count nunmber of elements in the list ; Declare temp pointer to traverse the list ; Variable to store the count ; Iterate the list and increment the count ; As the list is circular , increment the counter at last ; Function to insert a node at a given position in the circular doubly linked list ; Declare two pointers ; Create a new node in memory ; Point temp to start ; count of total elements in the list ; If list is empty or the position is not valid , return false ; Assign the data ; Iterate till the loc ; See in Image , circle 1 ; See in Image , Circle 2 ; See in Image , Circle 3 ; See in Image , Circle 4 ; Function to create circular doubly linked list from array elements ; Declare newNode and temporary pointer ; Iterate the loop until array length ; Create new node ; Assign the array data ; If it is first element Put that node prev and next as start as it is circular ; Find the last node ; Add the last node to make them in circular fashion ; Driver Code ; Array elements to create circular doubly linked list ; Start Pointer ; Create the List ; Display the list before insertion ; Inserting 8 at 3 rd position ; Display the list after insertion
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * next ; struct node * prev ; } ; struct node * getNode ( ) { return ( ( struct node * ) malloc ( sizeof ( struct node ) ) ) ; } 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 ; } } int countList ( struct node * start ) { struct node * temp = start ; int count = 0 ; while ( temp -> next != start ) { temp = temp -> next ; count ++ ; } count ++ ; return count ; } bool insertAtLocation ( struct node * start , int data , int loc ) { struct node * temp , * newNode ; int i , count ; newNode = getNode ( ) ; temp = start ; count = countList ( start ) ; if ( temp == NULL count < loc ) return false ; else { newNode -> data = data ; for ( i = 1 ; i < loc - 1 ; i ++ ) { temp = temp -> next ; } newNode -> next = temp -> next ; ( temp -> next ) -> prev = newNode ; temp -> next = newNode ; newNode -> prev = temp ; return true ; } return false ; } void createList ( int arr [ ] , int n , struct node * * start ) { struct node * newNode , * temp ; int i ; for ( i = 0 ; i < n ; i ++ ) { newNode = getNode ( ) ; newNode -> data = arr [ i ] ; if ( i == 0 ) { * start = newNode ; newNode -> prev = * start ; newNode -> next = * start ; } else { temp = ( * start ) -> prev ; temp -> next = newNode ; newNode -> next = * start ; newNode -> prev = temp ; temp = * start ; temp -> prev = newNode ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; struct node * start = NULL ; createList ( arr , n , & start ) ; displayList ( start ) ; insertAtLocation ( start , 8 , 3 ) ; displayList ( start ) ; return 0 ; }