Posted on by Kalkicode

construct a linked list from 2d matrix in typescript

Ts program for construct a linked list from 2d matrix. Here problem description and explanation.

``````// TypeScript program for
// Construct a linked list from 2D matrix
{
public data: number;
constructor(data: number)
{
this.data = data;
this.next = null;
this.down = null;
}
}
{
constructor()
{
// Set inital value
}
public display()
{
{
}
else
{
while (front != null)
{
right = front;
while (right != null)
{
console.log(right.data + "  ");
right = right.next;
}
console.log("\n");
// Visit to down node
front = front.down;
}
}
}
public insertData(matrix: number[][],
rows: number, cols: number)
{
// Some auxiliary variables
var i = 0;
var j = 0;
// Add first row elements into result list
while (i < cols)
{
{
}
else
{
}
i++;
}
// Get first element
i = 1;
// Add all the bottom element of each column
while (i < rows)
{
root = null;
perv = root;
while (j < cols)
{
if (root == null)
{
perv = root;
}
else
{
}
j++;
}
// reset col
j = 0;
// change row
i++;
}
}
public static main(args: string[])
{
// Create 2D metrix
var matrix: number[][] = [
[1, 6, 9, 2, -9],
[2, 5, -5, 7, 1],
[3, 4, 1, 8, 2]
];
var rows = matrix.length;
var cols = matrix[0].length;
dll.insertData(matrix, rows, cols);
dll.display();
}
}
/*
file : code.ts
tsc --target es6 code.ts
node code.js
*/``````

Output

``````1
6
9
2
-9

2
5
-5
7
1

3
4
1
8
2

``````

Comment

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.

Categories
Relative Post