# Construct a linked list from 2D matrix

Here given code implementation process.

``````//C Program
//Construct a linked list from 2D matrix
#include <stdio.h>
#include <stdlib.h> //for malloc function

#define ROW 3
#define COLS 5
//create structure
struct Node{
int data;
struct Node*next;
struct Node*down;
};

//insert Node element of end of linked list
struct Node* insert(int value){

//Create a dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL){
printf("Memory overflow\n");
exit(0);
}else{
//set data value
node->data=value;
node->next=NULL;
node->down=NULL;
}

return node;
}
//display element of Node

}
else{
struct Node*right_side=NULL;
//Traverse doubly linked list from front to rear
{

while(right_side!=NULL)
{
printf("%3d",right_side->data );
right_side=right_side->next;
}

printf("\n");
}
}

}

struct Node* insertData(int (*matrix)[COLS],int row,int cols)
{
for (int i = 0; i < cols; ++i)
{
{
}
else{
}
}

for (int i = 1; i < row; ++i)
{

root=NULL;
perv=root;
for (int j = 0; j < cols; ++j)
{

if(root==NULL)
{
perv=root;
}
else{

}

}

}

}
int main(){

//Create 2D metrix
int matrix[ROW][COLS]={
{1,6,9,2,-9},
{2,5,-5,7,1},
{3,4,-1,8,2}
};
//set node pointer value

return 0;
}```
```

#### Output

``````  1  6  9  2 -9
2  5 -5  7  1
3  4 -1  8  2
``````
``````/*
C++ Program
Construct a linked list from 2D matrix
*/

#include<iostream>

using namespace std;

#define COLS 5
class Node {
public:
int data;
Node *next;
Node *down;
Node(int value) {
this->data = value;
this->next = NULL;
this->down = NULL;
}
};
public:
Node *tail;
this->tail = NULL;
}
void display() {
} else {
Node *right_side = NULL;
while (front != NULL) {
right_side = front;
while (right_side != NULL) {
cout << right_side->data << "  ";
right_side = right_side->next;
}
cout << "\n";
front = front->down;
}
}
}
void insertData(int matrix[][COLS], int row, int cols) {
Node *levelHead = NULL, *root = NULL, *perv = NULL;
int i = 0;
int j = 0;
while (i < cols) {
} else {
}
i++;
}
i = 1;
while (i < row) {
root = NULL;
perv = root;
j = 0;
while (j < cols) {
if (root == NULL) {
perv = root;
} else {
}
j++;
}
i++;
}
}
};
int main() {
int matrix[][COLS] = {
{
1,
6,
9,
2,
-9
},
{
2,
5,
-5,
7,
1
},
{
3,
4,
1,
8,
2
}
};
int row = sizeof(matrix) / sizeof(matrix[0]);

obj.insertData(matrix, row, COLS);

obj.display();

return 0;
}```
```

#### Output

``````  1  6  9  2 -9
2  5 -5  7  1
3  4 -1  8  2
``````
``````/*
Java Program
Construct a linked list from 2D matrix
*/
class Node {

public int data;

public Node next;

public Node down;

public Node(int value) {
//Setup initial values of linked list node
this.data = value;
this.next = null;
this.down = null;
}
}

public Node tail;

tail = null;
}
public void display() {

} else {
Node right_side = null;

while (front != null) {

right_side = front;

while (right_side != null) {

System.out.print(right_side.data+"  ");
right_side = right_side.next;
}

System.out.print("\n");
front = front.down;
}
}
}
public void insertData(int[][] matrix, int row, int cols) {
Node levelHead = null, root = null, perv = null;
int i=0;
int j=0;
while (i < cols) {

} else {
}
i++;
}
i=1;
while ( i < row) {
root = null;
perv = root;
j=0;
while(j < cols) {

if (root == null) {
perv = root;
} else {
}
j++;
}

i++;
}
}

public static void main(String[] args) {

//Create 2D metrix
int[][] matrix = {
{
1, 6, 9, 2,  -9
},
{
2, 5, -5, 7, 1
},
{
3,  4, 1, 8, 2
}
};
int row = matrix.length;

int cols = matrix[0].length;

obj.insertData(matrix, row, cols);

obj.display();

}
}```
```

#### Output

``````  1  6  9  2 -9
2  5 -5  7  1
3  4 -1  8  2
``````
``````/*
C# Program
Construct a linked list from 2D matrix
*/
using System;
public class Node {

public int data;

public Node next;

public Node down;

public Node(int value) {
//Setup initial values of linked list node
this.data = value;
this.next = null;
this.down = null;
}
}

public Node tail;

tail = null;
}
public void display() {

} else {
Node right_side = null;

while (front != null) {

right_side = front;

while (right_side != null) {

Console.Write(right_side.data+"  ");
right_side = right_side.next;
}

Console.Write("\n");
front = front.down;
}
}
}
public void insertData(int[,] matrix, int row, int cols) {
Node levelHead = null, root = null, perv = null;
int i=0;
int j=0;
while (i < cols) {

} else {
}
i++;
}
i=1;
while ( i < row) {
root = null;
perv = root;
j=0;
while(j < cols) {

if (root == null) {
perv = root;
} else {
}
j++;
}

i++;
}
}

public static void Main(String[] args) {

//Create 2D metrix
int[,] matrix = {
{
1, 6, 9, 2,  -9
},
{
2, 5, -5, 7, 1
},
{
3,  4, 1, 8, 2
}
};
int row = matrix.GetLength(0);

int cols = matrix.GetLength(1);

obj.insertData(matrix, row, cols);

obj.display();

}
}```
```

#### Output

``````  1  6  9  2 -9
2  5 -5  7  1
3  4 -1  8  2
``````
``````# Python 3 Program
# Construct a linked list from 2D matrix

class Node :

def __init__(self, value) :
self.data = value
self.next = None
self.down = None

def __init__(self) :
self.tail = None

def display(self) :
else :
right_side = None
while (front != None) :
right_side = front
while (right_side != None) :
print(right_side.data ,end="  ")
right_side = right_side.next

print(end="\n")
front = front.down

def insertData(self, matrix, row, cols) :
root = None
perv = None
i = 0
j = 0
while (i < cols) :
else :

i += 1

i = 1
while (i < row) :
root = None
perv = root
j = 0
while (j < cols) :
if (root == None) :
perv = root
else :

j += 1

i += 1

def main() :
matrix = [
[1, 6, 9, 2, -9],
[2, 5, -5, 7, 1],
[3, 4, 1, 8, 2]
]
row = len(matrix)
cols = len(matrix[0])
obj.insertData(matrix, row, cols)
obj.display()

if __name__ == "__main__":
main()```
```

#### Output

``````1  6  9  2  -9
2  5  -5  7  1
3  4  1  8  2  ``````
``````# Ruby Program
# Construct a linked list from 2D matrix

class Node
attr_accessor :data, :next, :down
def initialize(value)
self.data = value
self.next = nil
self.down = nil
end
end

def initialize()
@tail = nil
end
def display()
else
right_side = nil
while (front != nil)
right_side = front
while (right_side != nil)
print(right_side.data ,"  ")
right_side = right_side.next
end
print("\n")
front = front.down
end
end
end
def insertData(matrix, row, cols)
root = nil
perv = nil
i = 0
j = 0
while (i < cols)
else
end
i += 1
end
i = 1
while (i < row)
root = nil
perv = root
j = 0
while (j < cols)
if (root == nil)
perv = root
else
end
j += 1
end
i += 1
end
end
end
def main()
matrix = [
[1, 6, 9, 2, -9],
[2, 5, -5, 7, 1],
[3, 4, 1, 8, 2]
]
row = matrix.length
cols = matrix[0].length
obj.insertData(matrix, row, cols)
obj.display()
end

main()```
```

#### Output

``````1  6  9  2  -9
2  5  -5  7  1
3  4  1  8  2  ``````
``````<?php

/*
Php Program
Construct a linked list from 2D matrix
*/

class Node {
public \$data;
public \$next;
public \$down;

function __construct(\$value) {
\$this->data = \$value;
\$this->next = null;
\$this->down = null;
}
}
public \$tail;

function __construct() {
\$this->tail = null;
}
public  function display() {
} else {
\$right_side = null;
while (\$front != null) {
\$right_side = \$front;
while (\$right_side != null) {
echo(\$right_side->data ."  ");
\$right_side = \$right_side->next;
}
echo("\n");
\$front = \$front->down;
}
}
}
public  function insertData(\$matrix, \$row, \$cols) {
\$root = null;
\$perv = null;
\$i = 0;
\$j = 0;
while (\$i < \$cols) {
} else {
}
\$i++;
}
\$i = 1;
while (\$i < \$row) {
\$root = null;
\$perv = \$root;
\$j = 0;
while (\$j < \$cols) {
if (\$root == null) {
\$perv = \$root;
} else {
}
\$j++;
}
\$i++;
}
}
}

function main() {
\$matrix = array(array(1, 6, 9, 2, -9), array(2, 5, -5, 7, 1), array(3, 4, 1, 8, 2)
);
\$row = count(\$matrix);
\$cols = count(\$matrix[0]);
\$obj->insertData(\$matrix, \$row, \$cols);
\$obj->display();
}
main();```
```

#### Output

``````1  6  9  2  -9
2  5  -5  7  1
3  4  1  8  2  ``````
``````/*
Node Js Program
Construct a linked list from 2D matrix
*/

class Node {

constructor(value) {
this.data = value;
this.next = null;
this.down = null;
}
}

constructor() {
this.tail = null;
}
display() {
} else {
var right_side = null;
while (front != null) {
right_side = front;
while (right_side != null) {
process.stdout.write(right_side.data + "  ");
right_side = right_side.next;
}
process.stdout.write("\n");
front = front.down;
}
}
}
insertData(matrix, row, cols) {
var root = null;
var perv = null;
var i = 0;
var j = 0;
while (i < cols) {
} else {
}
i++;
}
i = 1;
while (i < row) {
root = null;
perv = root;
j = 0;
while (j < cols) {
if (root == null) {
perv = root;
} else {
}
j++;
}
i++;
}
}
}

function main() {
var matrix = [
[1, 6, 9, 2, -9],
[2, 5, -5, 7, 1],
[3, 4, 1, 8, 2]
];
var row = matrix.length;
var cols = matrix[0].length;
obj.insertData(matrix, row, cols);
obj.display();
}

main();```
```

#### Output

``````1  6  9  2  -9
2  5  -5  7  1
3  4  1  8  2  ``````
``````/*
Swift 4 Program
Construct a linked list from 2D matrix
*/

class Node {
var data: Int;
var next: Node? ;
var down: Node? ;
init(_ value: Int) {
self.data = value;
self.next = nil;
self.down = nil;
}
}
var tail: Node? ;
init() {
self.tail = nil;
}
func display() {
} else {
var right_side: Node? = nil;
while (front != nil) {
right_side = front;
while (right_side != nil) {
print(right_side!.data ,terminator:"  ");
right_side = right_side!.next;
}
print(terminator:"\n");
front = front!.down;
}
}
}
func insertData(_ matrix: [[Int]] , _ row : Int, _ cols: Int) {
var root: Node?  = nil;
var perv: Node?  = nil;
var i: Int = 0;
var j: Int = 0;
while (i < cols) {
} else {
}
i += 1;
}
i = 1;
while (i < row) {
root = nil;
perv = root;
j = 0;
while (j < cols) {
if (root == nil) {
perv = root;
} else {
}
j += 1;
}
i += 1;
}
}
}
func main() {
let matrix: [[Int]] = [
[1, 6, 9, 2, -9],
[2, 5, -5, 7, 1],
[3, 4, 1, 8, 2]
];
let row: Int = matrix.count;
let cols: Int = matrix[0] .count;
obj.insertData(matrix, row, cols);
obj.display();
}
main();```
```

#### Output

``````1  6  9  2  -9
2  5  -5  7  1
3  4  1  8  2  ``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.