forked from goyalavishi/Practice-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparsematrix.cpp
95 lines (85 loc) · 1.65 KB
/
sparsematrix.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
#include<iostream>
using namespace std;
#include<vector>
class Node{
public:
int row=0;
int col=0;
int value=0;
Node* next=NULL;
Node(int row,int col,int value,Node* next)
{
this->row=row;
this->col=col;
this->value=value;
this->next=next;
}
};
void createnewnode(int row,int col,int value,Node*& start)
{
if(start==NULL)
{
start=new Node(row,col,value,NULL);
}
else if(start->next==NULL){
Node* nn=new Node(row,col,value,NULL);
start->next=nn;
}
else{
Node* temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
Node *nn = new Node(row, col, value, NULL);
temp->next = nn;
}
}
void display(Node* start)
{
Node* rp=start;
Node *cp = start;
Node *vp = start;
cout<<" Row position: ";
while(rp!=NULL)
{
cout<<rp->row<<" ";
rp=rp->next;
}
cout<<endl;
cout << " Column position: ";
while (cp != NULL)
{
cout << cp->col << " ";
cp = cp->next;
}
cout << endl;
cout << " Value : ";
while (vp != NULL)
{
cout << vp->value << " ";
vp = vp->next;
}
cout << endl;
}
int main(int argc,char** argv)
{
Node* start=NULL;
vector<vector<int>> v{
{0, 0, 3, 0, 4},
{0, 0, 5, 7, 0},
{0, 0, 0, 0, 0},
{0, 2, 6, 0, 0}
};
for (int i = 0; i <v.size();i++)
{
for(int j=0;j<v[0].size();j++)
{
if(v[i][j]!=0)
{
createnewnode(i,j,v[i][j],start);
}
}
}
display(start);
}