Insertion at Specific Position in a Circular Doubly Linked List | C # program to convert insert an element at a specific position in a circular doubly linked listing , end and middle ; 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
using System ; class GFG { public class node { public int data ; public node next ; public node prev ; } ; static node getNode ( ) { return new node ( ) ; } 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 ; } } static int countList ( node start ) { node temp = start ; int count = 0 ; while ( temp . next != start ) { temp = temp . next ; count ++ ; } count ++ ; return count ; } static node insertAtLocation ( node start , int data , int loc ) { node temp , newNode ; int i , count ; newNode = getNode ( ) ; temp = start ; count = countList ( start ) ; if ( temp == null count < loc ) return start ; 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 start ; } } static node createList ( int [ ] arr , int n , node start ) { 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 ; } } return start ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; node start = null ; start = createList ( arr , n , start ) ; displayList ( start ) ; start = insertAtLocation ( start , 8 , 3 ) ; displayList ( start ) ; } }