-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSparseMatrix.cpp
213 lines (204 loc) · 5.18 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class List
{
private:
int index;
int value;
List *nextindex;
public:
List(int index)
{
this->index = index;
nextindex = NULL;
value = NULL;
}
List()
{
index = -1;
value = NULL;
nextindex = NULL;
}
void store(int index, int value)
{
List *current = this;
List *previous = NULL;
List *node = new List(index);
node->value = value;
while (current != NULL && current->index < index)
{
previous = current;
current = current->nextindex;
}
if (current == NULL)
{
previous->nextindex = node;
}
else
{
if (current->index == index)
{
cout<<"DUPLICATE INDEX"<<endl;
return;
}
previous->nextindex = node;
node->nextindex = current;
}
return;
}
int fetch(int index)
{
List *current = this;
int value = NULL;
while (current != NULL && current->index != index)
{
current = current->nextindex;
}
if (current != NULL)
{
value = current->value;
}
else
{
value = NULL;
}
return value;
}
int elementCount()
{
int elementCount = 0;
List *current = this->nextindex;
for ( ; (current != NULL); current = current->nextindex)
{
elementCount++;
}
return elementCount;
}
};
class SparseArray
{
private:
List *start;
int index;
public:
SparseArray(int index)
{
start = new List();
this->index = index;
}
void store(int index, int value)
{
if (index >= 0 && index < this->index)
{
if (value != NULL)
start->store(index, value);
}
else
{
cout<<"INDEX OUT OF BOUNDS"<<endl;
}
}
int fetch(int index)
{
if (index >= 0 && index < this->index)
return start->fetch(index);
else
{
cout<<"INDEX OUT OF BOUNDS"<<endl;
return NULL;
}
}
int elementCount()
{
return start->elementCount();
}
};
class SparseMatrix
{
private:
int N;
SparseArray **sparsearray;
public:
SparseMatrix(int N)
{
this->N = N;
sparsearray = new SparseArray* [N];
for (int index = 0; index < N; index++)
{
sparsearray[index] = new SparseArray(N);
}
}
void store(int rowindex, int colindex, int value)
{
if (rowindex < 0 || rowindex > N)
{
cout<<"row index out of bounds"<<endl;
return;
}
if (colindex < 0 || colindex > N)
{
cout<<"col index out of bounds"<<endl;
return;
}
sparsearray[rowindex]->store(colindex, value);
}
int get(int rowindex, int colindex)
{
if (rowindex < 0 || colindex > N)
{
cout<<"row index out of bounds"<<endl;
return 0;
}
if (rowindex < 0 || colindex > N)
{
cout<<"col index out of bounds"<<endl;
return 0;
}
return (sparsearray[rowindex]->fetch(colindex));
}
int elementCount()
{
int count = 0;
for (int index = 0; index < N; index++)
{
count += sparsearray[index]->elementCount();
}
return count;
}
};
int main()
{
int iarray[3][3];
iarray[0][0] = 1;
iarray[0][1] = NULL;
iarray[0][2] = 2;
iarray[1][0] = NULL;
iarray[1][1] = 3;
iarray[1][2] = NULL;
iarray[2][0] = 4;
iarray[2][1] = 6;
iarray[2][2] = NULL;
SparseMatrix sparseMatrix(3);
for (int rowindex = 0; rowindex < 3; rowindex++)
{
for (int colindex = 0; colindex < 3; colindex++)
{
sparseMatrix.store(rowindex, colindex, iarray[rowindex][colindex]);
}
}
cout<<"the sparse Matrix is: "<<endl;
for (int rowindex = 0; rowindex < 3; rowindex++)
{
for (int colindex = 0; colindex < 3; colindex++)
{
if (sparseMatrix.get(rowindex, colindex) == NULL)
cout<<"NULL"<< "\t";
else
cout<<sparseMatrix.get(rowindex, colindex) << "\t";
}
cout<<endl;
}
cout<<"The Size of Sparse Matrix is "<<sparseMatrix.elementCount()<<endl;
}