Form a Rectangle from boundary elements of Matrix using Linked List | Python program for above approach Node Class ; Constructor to initialize the node object ; Linked List class ; Constructor to initialize head ; function to form square linked list of matrix . ; initialising A [ 0 ] [ 0 ] as head . ; head is assigned to head . ; i is row index , j is column index ; loop till temp . top become equal to head . ; iterating over first i . e 0 th row and connecting node . ; iterating over last i . e ( m - 1 ) th column and connecting Node . ; iterating over last i . e ( n - 1 ) th row and connecting Node . ; iterating over first i . e 0 th column and connecting Node . ; function to print Linked list . ; printing head of linked list ; loop till temp . top become equal to head ; printing the node ; Driver Code ; n is number of rows ; m is number of column ; creation of object ; Call Quad method to create Linked List . ; Call printList method to print list .
class Node : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . data = val NEW_LINE self . next = None NEW_LINE self . prev = None NEW_LINE self . top = None NEW_LINE self . bottom = None NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT def Quad ( self , grid , n , m ) : NEW_LINE INDENT self . head = Node ( grid [ 0 ] [ 0 ] ) NEW_LINE temp = self . head NEW_LINE i = 0 NEW_LINE j = 1 NEW_LINE while temp . top != self . head : NEW_LINE INDENT if j < m and i == 0 : NEW_LINE INDENT temp . next = Node ( grid [ i ] [ j ] ) NEW_LINE temp = temp . next NEW_LINE j += 1 NEW_LINE DEDENT elif j == m and i < n - 1 : NEW_LINE INDENT i = i + 1 NEW_LINE temp . bottom = Node ( grid [ i ] [ j - 1 ] ) NEW_LINE temp = temp . bottom NEW_LINE DEDENT elif i == n - 1 and j <= m and j >= 1 : NEW_LINE INDENT if j == m : j = j - 1 NEW_LINE j = j - 1 NEW_LINE temp . prev = Node ( grid [ i ] [ j ] ) NEW_LINE temp = temp . prev NEW_LINE DEDENT elif i <= n - 1 and j == 0 : NEW_LINE INDENT i = i - 1 NEW_LINE temp . top = Node ( grid [ i ] [ j ] ) NEW_LINE temp = temp . top NEW_LINE if i == 1 : NEW_LINE INDENT temp . top = self . head NEW_LINE DEDENT DEDENT DEDENT DEDENT def printList ( self , root ) : NEW_LINE INDENT temp = root NEW_LINE print ( temp . data , end = " ▁ " ) NEW_LINE while temp . top != root : NEW_LINE INDENT if temp . next : NEW_LINE INDENT print ( temp . next . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT if temp . prev : NEW_LINE INDENT print ( temp . prev . data , end = " ▁ " ) NEW_LINE temp = temp . prev NEW_LINE DEDENT if temp . bottom : NEW_LINE INDENT print ( temp . bottom . data , end = " ▁ " ) NEW_LINE temp = temp . bottom NEW_LINE DEDENT if temp . top : NEW_LINE INDENT print ( temp . top . data , end = " ▁ " ) NEW_LINE temp = temp . top NEW_LINE DEDENT DEDENT DEDENT DEDENT grid = [ [ 13 , 42 , 93 , 88 ] , [ 26 , 38 , 66 , 42 ] , [ 75 , 63 , 78 , 12 ] ] NEW_LINE n = len ( grid ) NEW_LINE m = len ( grid [ 0 ] ) NEW_LINE l = LinkedList ( ) NEW_LINE l . Quad ( grid , n , m ) NEW_LINE l . printList ( l . head ) NEW_LINE