Construct a Doubly linked linked list from 2D Matrix | define dimension of matrix ; struct node of doubly linked list with four pointer next , prev , up , down ; function to create a new node ; function to construct the doubly linked list ; Create Node with value contain in matrix at index ( i , j ) ; Assign address of curr into the prev pointer of temp ; Assign address of curr into the up pointer of temp ; Recursive call for next pointer ; Recursive call for down pointer ; Return newly constructed node whose all four node connected at it 's appropriate position ; Function to construct the doubly linked list ; function call for construct the doubly linked list ; function for displaying doubly linked list data ; pointer to move right ; pointer to move down ; loop till node -> down is not NULL ; loop till node -> right is not NULL ; Driver code ; initialise matrix
dim = 3 NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . prev = None NEW_LINE self . up = None NEW_LINE self . down = None NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def createNode ( data ) : NEW_LINE INDENT temp = Node ( data ) ; NEW_LINE return temp ; NEW_LINE DEDENT def constructDoublyListUtil ( mtrx , i , j , curr ) : NEW_LINE INDENT if ( i >= dim or j >= dim ) : NEW_LINE INDENT return None ; NEW_LINE DEDENT temp = createNode ( mtrx [ i ] [ j ] ) ; NEW_LINE temp . prev = curr ; NEW_LINE temp . up = curr ; NEW_LINE temp . next = constructDoublyListUtil ( mtrx , i , j + 1 , temp ) ; NEW_LINE temp . down = constructDoublyListUtil ( mtrx , i + 1 , j , temp ) ; NEW_LINE return temp ; NEW_LINE DEDENT def constructDoublyList ( mtrx ) : NEW_LINE INDENT return constructDoublyListUtil ( mtrx , 0 , 0 , None ) ; NEW_LINE DEDENT def display ( head ) : NEW_LINE INDENT rPtr = None NEW_LINE dPtr = head ; NEW_LINE while ( dPtr != None ) : NEW_LINE INDENT rPtr = dPtr ; NEW_LINE while ( rPtr != None ) : NEW_LINE INDENT print ( rPtr . data , end = ' ▁ ' ) NEW_LINE rPtr = rPtr . next ; NEW_LINE DEDENT print ( ) NEW_LINE dPtr = dPtr . down ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mtrx = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE list = constructDoublyList ( mtrx ) ; NEW_LINE display ( list ) ; NEW_LINE DEDENT