-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkListToNDMatrix.cpp
More file actions
58 lines (45 loc) · 1.22 KB
/
Copy pathLinkListToNDMatrix.cpp
File metadata and controls
58 lines (45 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
struct Node
{
int data;
Node* right, *down;
Node(int x){
data = x;
right = NULL;
down = NULL;
}
};
// n is the size of the matrix
// function must return the pointer to the first element
// of the in linked list i.e. that should be the element at arr[0][0]
Node* constructLinkedMatrix(int mat[MAX][MAX], int n)
{
// code here
Node *head = new Node(mat[0][0]);
Node *traverse = head;
Node *Htraverse = head;
Node *Vtraverse = head;
Node *BindRight,*BindDown;
BindDown = BindRight = head;
for(int cols=1,rows = 1;cols<n && rows<n;cols++,rows++)
{
Htraverse->right = mat[0][cols];
Htraverse = Htraverse->right;
Vtraverse->down = mat[rows][0];
Vtraverse = Vtraverse->down;
}
BindRight = head->right;
BindDown = head->down;
for(int rows = 1;rows<n;rows++)
{
Node *horizontalCols = BindRight,*horizontalRows = BindDown;
for(int cols=1;cols<n;cols++)
{
horizontalRows->right = horizontalCols->down = new Node(mat[rows][cols]);
horizontalRows = horizontalRows->right;
horizontalCols = horizontalCols->right;
}
BindRight = BindRight->down;
BindDown = BindDown->down;
}
return head;
}