-
Notifications
You must be signed in to change notification settings - Fork 8
/
list-matrix.cpp
executable file
·96 lines (89 loc) · 1.59 KB
/
list-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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Given a matrix. Convert it into a linked list matrix such that each node is connected to its next right and down node.
Ex:
1 2 3
4 5 6
7 8 9
Output:
1->2->3->NULL
| | |
v v v
4->5->6->NULL
| | |
v v v
7->8->9->NULL
| | |
v v v
--NULL-
http://www.geeksforgeeks.org/factset-interview-experience-set-9-campus-full-time/
*/
#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
node *right, *down;
};
// utility function
// returns new node of type node
node *newnode(int data){
node *temp = new node();
temp->data = data;
temp->right = NULL;
temp->down = NULL;
return temp;
}
node *construct(int arr[][3]){
int n = 3, m = 3;
node *head = newnode(arr[0][0]);
node *p = head;
node *temp;// = p->down;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
//if(j != m-1){
if(j != m-1) p->right = newnode(arr[i][j+1]);
if(i != n-1) p->down = newnode(arr[i+1][j]);
if(j == 0){
temp = p->down;
}
p = p->right;
//}
if(j == m-1 && i == n-1){
break;
}
else if(j == m-1){
p = temp;
if(temp->down) temp = temp->down;
}
}
}
return head;
}
// utility function for
// display of linked list
void display(node *head){
node *p = head;
node *temp = NULL;
if(p->down) temp = p->down;
while(p->down || p->right){
cout << p->data;
if(p->right){
p = p->right;
cout << " ";
}else if(p->down){
p = temp;
if(temp->down) temp = temp->down;
cout << endl;
}
}
}
// driver program
int main(){
int arr[][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
node *head = construct(arr);
display(head);
return 0;
}