Convert an Array to a Circular Doubly Linked List | Doubly linked list node ; Utility function to create a node in memory ; Function to display the list ; Function to convert array into list ; 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 ; Array to be converted ; Start Pointer ; Create the List ; Display the list
class node { constructor ( ) { this . data = 0 ; this . next = this . prev = null ; } } function getNode ( ) { return new node ( ) ; } function displayList ( temp ) { let t = temp ; if ( temp == null ) return 0 ; else { document . write ( " " ) ; while ( temp . next != t ) { document . write ( temp . data + " " ) ; temp = temp . next ; } document . write ( temp . data ) ; return 1 ; } } function createList ( arr , n , start ) { let newNode , temp ; let 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 ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let n = arr . length ; let start = null ; start = createList ( arr , n , start ) ; displayList ( start ) ;