📜  使用链表从矩阵的边界元素形成一个矩形

📅  最后修改于: 2022-05-13 01:56:07.707000             🧑  作者: Mango

使用链表从矩阵的边界元素形成一个矩形

给定一个大小为NxM的矩阵grid[][] ,其中N是行数, M是列数。任务是使用具有四个指针即prevnexttopbottom的链表从grid[][]的边界元素形成一个矩形。打印最终的链表。

例子:

方法:这个问题可以通过执行矩阵的边界遍历并为每个元素创建节点并使用next、prev、bottom或top链接它们并创建一个链表来解决。

请按照以下步骤操作:

步骤 1:grid[0][0]作为链表的头部,并将temp初始化为头部
第 2 步:j=1遍历第一行到j=m-1 ,其中i=0 ,并为每个元素创建一个节点,并通过next指针链接它们。
第 3 步:i=0遍历最后一列到i=n-1 ,其中j=m-1并为每个元素创建一个节点,并通过底部指针链接它们。
第 4 步:j=m-1遍历最后一行到j=0 ,其中i=n-1并为每个元素创建一个节点,并通过prev指针链接它们。
第 5 步:i=n-1遍历第一列到i=0 ,其中j=0 ,并为每个元素创建一个节点,并通过顶部指针链接它们。
第 6 步:重复第 2、3、4、5 步,直到temp.top等于head
第 7 步:打印所需的链表。

下面是上述算法的实现。

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


Javascript


输出
13 42 93 88 42 12 78 63 75 26 

时间复杂度: O(N*M)
辅助空间: O(N*M)