-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinklist_Matrix.cpp
45 lines (40 loc) · 994 Bytes
/
Linklist_Matrix.cpp
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
/*structure of the node of the linked list is as
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* temp = head;
for(int i=0;i<n;i++)
{
Node* temp2 = temp;
for(int j=0;j<n;j++)
{
if(j+1 == n)
temp->right = NULL;
else
temp->right = new Node(mat[i][j+1]);
if(i+1 == n)
temp->down = NULL;
else
temp->down = new Node(mat[i+1][j]);
temp = temp->right;
}
temp = temp2;
temp = temp->down;
}
return head;
}