Form a Rectangle from boundary elements of Matrix using Linked List
Given a Matrix grid[][] of size NxM where N is number of rows and M is number of columns. The task is to form a rectangle from boundary elements of grid[][] using linked list having four pointers namely prev, next, top and bottom. Print the final linked list.
Examples:
Input: A = [[13, 42, 93, 88],
[26, 38, 66, 42],
[75, 63, 78, 12]]
Output: 13 42 93 88 42 12 78 63 75 26
Explanation:
1. Make A[0][0] and head node
2. Traverse through 0th row and create node for each element and connect them through next pointer.
3. Traverse through (m-1)th column and create node for each element and connect them through bottom pointer.
4. Traverse through (n-1)th row and create node for each element and connect them through prev pointer.
5. Traverse through 0th column and create node for each element and connect them through top pointer.
6. Step 2, 3, 4, 5 is repeated till temp. The top become equal to head.
Input: A = [[1, 2, 3]
[8, 9, 4]
[7, 6, 5]]
Output: 1 2 3 4 5 6 7 8
Approach: This problem can be solved by Performing boundary traversal of matrix and creating nodes for each element and link them using next, prev, bottom or top and create a linked list.
Follow the steps below:
Step 1: Make grid[0][0] as the head of the Linked list and initialize temp as the head.
Step 2: Traverse through the first row from j=1 to j=m-1 where i=0 and create a node for each element and link them through the next pointer.
Step 3: Traverse through the last column from i=0 to i=n-1 where j=m-1 and create a node for each element and link them through a bottom pointer.
Step 4: Traverse through the last row from j=m-1 to j=0 where i=n-1 and create a node for each element and link them through the prev pointer.
Step 5: Traverse through the first column from i=n-1 to i=0 where j=0 and create a node for each element and link them through the top pointer.
Step 6: Step 2, 3, 4, 5 is repeated till temp.top becomes equal to head.
Step 7: Print the required Linked List.
Below is the implementation of the above algorithm.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
Node* top;
Node* bottom;
Node( int data)
{
this ->data = data;
next = NULL;
prev = NULL;
top = NULL;
bottom = NULL;
}
};
struct LinkedList {
Node* head = NULL;
void Quad(vector<vector< int > > grid, int n, int m)
{
head = new Node(grid[0][0]);
Node* temp = head;
int i = 0;
int j = 1;
while (temp != NULL and temp->top != head) {
if (j < m && i == 0) {
temp->next = new Node(grid[i][j]);
temp = temp->next;
j += 1;
}
else if (j == m && i < n - 1) {
i = i + 1;
temp->bottom = new Node(grid[i][j - 1]);
temp = temp->bottom;
}
else if (i == n - 1 && j <= m && j >= 1) {
if (j == m)
j = j - 1;
j = j - 1;
temp->prev = new Node(grid[i][j]);
temp = temp->prev;
}
else if (i <= n - 1 && j == 0) {
i = i - 1;
temp->top = new Node(grid[i][j]);
temp = temp->top;
if (i == 1)
temp->top = head;
}
}
}
void printList(Node* root)
{
Node* temp = root;
cout << temp->data << " " ;
while (temp->top != root) {
if (temp->next) {
cout << temp->next->data << " " ;
temp = temp->next;
}
if (temp->prev) {
cout << temp->prev->data << " " ;
temp = temp->prev;
}
if (temp->bottom) {
cout << temp->bottom->data << " " ;
temp = temp->bottom;
}
if (temp->top) {
cout << temp->top->data << " " ;
temp = temp->top;
}
}
}
};
int main()
{
vector<vector< int > > grid = { { 13, 42, 93, 88 },
{ 26, 38, 66, 42 },
{ 75, 63, 78, 12 } };
int n = grid.size();
int m = grid[0].size();
LinkedList* l = new LinkedList();
l->Quad(grid, n, m);
l->printList(l->head);
return 0;
}
|
Java
class Node {
int data;
Node next;
Node prev;
Node top;
Node bottom;
Node( int data)
{
this .data = data;
next = null ;
prev = null ;
top = null ;
bottom = null ;
}
}
class GFG {
public Node head = null ;
public void Quad( int [][] grid, int n, int m)
{
head = new Node(grid[ 0 ][ 0 ]);
Node temp = head;
int i = 0 ;
int j = 1 ;
while (temp.top != head) {
if (j < m && i == 0 ) {
temp.next = new Node(grid[i][j]);
temp = temp.next;
j += 1 ;
}
else if (j == m && i < n - 1 ) {
i = i + 1 ;
temp.bottom = new Node(grid[i][j - 1 ]);
temp = temp.bottom;
}
else if (i == n - 1 && j <= m && j >= 1 ) {
if (j == m) {
j = j - 1 ;
}
j = j - 1 ;
temp.prev = new Node(grid[i][j]);
temp = temp.prev;
}
else if (i <= n - 1 && j == 0 ) {
i = i - 1 ;
temp.top = new Node(grid[i][j]);
temp = temp.top;
if (i == 1 ) {
temp.top = head;
}
}
}
}
public void printList(Node root)
{
Node temp = root;
System.out.print(temp.data + " " );
while (temp.top != root) {
if (temp.next != null ) {
System.out.print(temp.next.data + " " );
temp = temp.next;
}
if (temp.prev != null ) {
System.out.print(temp.prev.data + " " );
temp = temp.prev;
}
if (temp.bottom != null ) {
System.out.print(temp.bottom.data + " " );
temp = temp.bottom;
}
if (temp.top != null ) {
System.out.print(temp.top.data + " " );
temp = temp.top;
}
}
}
public static void main(String[] args)
{
int [][] grid = new int [][] { { 13 , 42 , 93 , 88 },
{ 26 , 38 , 66 , 42 },
{ 75 , 63 , 78 , 12 } };
int n = grid.length;
int m = grid[ 0 ].length;
GFG l = new GFG();
l.Quad(grid, n, m);
l.printList(l.head);
}
}
|
Python3
class Node:
def __init__( self , val):
self .data = val
self . next = None
self .prev = None
self .top = None
self .bottom = None
class LinkedList:
def __init__( self ):
self .head = None
def Quad( self , grid, n, m):
self .head = Node(grid[ 0 ][ 0 ])
temp = self .head
i = 0
j = 1
while temp.top ! = self .head:
if j < m and i = = 0 :
temp. next = Node(grid[i][j])
temp = temp. next
j + = 1
elif j = = m and i < n - 1 :
i = i + 1
temp.bottom = Node(grid[i][j - 1 ])
temp = temp.bottom
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
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
def printList( self , root):
temp = root
print (temp.data, end = " " )
while temp.top ! = root:
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
grid = [[ 13 , 42 , 93 , 88 ],
[ 26 , 38 , 66 , 42 ],
[ 75 , 63 , 78 , 12 ]]
n = len (grid)
m = len (grid[ 0 ])
l = LinkedList()
l.Quad(grid, n, m)
l.printList(l.head)
|
C#
using System;
class Node {
public int data;
public Node next;
public Node prev;
public Node top;
public Node bottom;
public Node( int data)
{
this .data = data;
this .next = null ;
this .prev = null ;
this .top = null ;
this .bottom = null ;
}
}
public class GFG {
Node head = null ;
public void Quad( int [, ] grid, int n, int m)
{
head = new Node(grid[0, 0]);
Node temp = head;
int i = 0;
int j = 1;
while (temp.top != head)
{
if (j < m && i == 0) {
temp.next = new Node(grid[i, j]);
temp = temp.next;
j += 1;
}
else if (j == m && i < n - 1) {
i = i + 1;
temp.bottom = new Node(grid[i, j - 1]);
temp = temp.bottom;
}
else if (i == n - 1 && j <= m && j >= 1) {
if (j == m) {
j = j - 1;
}
j = j - 1;
temp.prev = new Node(grid[i, j]);
temp = temp.prev;
}
else if (i <= n - 1 && j == 0) {
i = i - 1;
temp.top = new Node(grid[i, j]);
temp = temp.top;
if (i == 1) {
temp.top = head;
}
}
}
}
void printList(Node root)
{
Node temp = root;
Console.Write(temp.data + " " );
while (temp.top != root) {
if (temp.next != null ) {
Console.Write(temp.next.data + " " );
temp = temp.next;
}
if (temp.prev != null ) {
Console.Write(temp.prev.data + " " );
temp = temp.prev;
}
if (temp.bottom != null ) {
Console.Write(temp.bottom.data + " " );
temp = temp.bottom;
}
if (temp.top != null ) {
Console.Write(temp.top.data + " " );
temp = temp.top;
}
}
}
static public void Main()
{
int [, ] grid = { { 13, 42, 93, 88 },
{ 26, 38, 66, 42 },
{ 75, 63, 78, 12 } };
int n = grid.GetLength(0);
int m = grid.GetLength(1);
GFG l = new GFG();
l.Quad(grid, n, m);
l.printList(l.head);
}
}
|
Javascript
<script>
class Node{
constructor(val){
this .data = val
this .next = null
this .prev = null
this .top = null
this .bottom = null
}
}
class LinkedList{
constructor(){
this .head = null
}
Quad(grid, n, m){
this .head = new Node(grid[0][0])
let temp = this .head
let i = 0
let j = 1
while (temp.top != this .head){
if (j < m && i == 0){
temp.next = new Node(grid[i][j])
temp = temp.next
j += 1
}
else if (j == m && i < n - 1){
i = i + 1
temp.bottom = new Node(grid[i][j - 1])
temp = temp.bottom
}
else if (i == n - 1 && j <= m && j >= 1){
if (j == m) j = j - 1
j = j - 1
temp.prev = new Node(grid[i][j])
temp = temp.prev
}
else if (i <= n - 1 && j == 0){
i = i - 1
temp.top = new Node(grid[i][j])
temp = temp.top
if (i == 1)
temp.top = this .head
}
}
}
printList(root){
let temp = root
document.write(temp.data + " " )
while (temp.top != root){
if (temp.next){
document.write(temp.next.data + " " )
temp = temp.next
}
if (temp.prev){
document.write(temp.prev.data + " " )
temp = temp.prev
}
if (temp.bottom){
document.write(temp.bottom.data + " " )
temp = temp.bottom
}
if (temp.top){
document.write(temp.top.data + " " )
temp = temp.top
}
}
}
}
let grid = [[13, 42, 93, 88],
[26, 38, 66, 42],
[75, 63, 78, 12]]
let n = grid.length
let m = grid[0].length
let l = new LinkedList()
l.Quad(grid, n, m)
l.printList(l.head)
</script>
|
Output
13 42 93 88 42 12 78 63 75 26
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
Last Updated :
22 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...