Convert an Array to a Circular Doubly Linked List | Node of the doubly linked list ; 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 ; Driver Code ; Array to be converted ; Start Pointer ; Create the List ; Display the list
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . prev = None NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getNode ( ) : NEW_LINE INDENT return ( Node ( 0 ) ) NEW_LINE DEDENT def displayList ( temp ) : NEW_LINE INDENT t = temp NEW_LINE if ( temp == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ list ▁ is : ▁ " , end = " ▁ " ) NEW_LINE while ( temp . next != t ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT print ( temp . data ) NEW_LINE return 1 NEW_LINE DEDENT DEDENT def createList ( arr , n , start ) : NEW_LINE INDENT newNode = None NEW_LINE temp = None NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT newNode = getNode ( ) NEW_LINE newNode . data = arr [ i ] NEW_LINE if ( i == 0 ) : NEW_LINE INDENT start = newNode NEW_LINE newNode . prev = start NEW_LINE newNode . next = start NEW_LINE DEDENT else : NEW_LINE INDENT temp = ( start ) . prev NEW_LINE temp . next = newNode NEW_LINE newNode . next = start NEW_LINE newNode . prev = temp NEW_LINE temp = start NEW_LINE temp . prev = newNode NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return start NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE start = None NEW_LINE start = createList ( arr , n , start ) NEW_LINE displayList ( start ) NEW_LINE DEDENT