-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-Sparse-Matrix.cpp
281 lines (252 loc) · 6.75 KB
/
04-Sparse-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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Implement an efficient data structure for sparse matrices.
date:2020-10-26
*/
#include <iostream>
using namespace std;
class llNode {
private:
int value;
int row_pos;
int column_pos;
llNode *next;
public:
llNode() : next(NULL){};
llNode(int a) : value(a), next(NULL){};
friend class LinkedList;
};
class LinkedList {
private:
llNode *first;
public:
LinkedList() : first(NULL){};
void PrintList();
void addNode(int newrow_pos, int newcolumn_pos, int newvalue);
void printMatrix(int m, int n);
LinkedList matrixAdd(LinkedList A, LinkedList B);
int search(LinkedList A, int row_index, int column_index);
LinkedList matrixMultiple(LinkedList A, LinkedList B, int m, int n, int l);
};
int main() {
int m, n, l, pos, value;
int operation;
LinkedList list1, list2, list3;
cout << "Do addition, press 1; do multiplication, press 2:" << endl;
cin >> operation;
if (operation == 1) {
cout << "Enter the number of row and column:" << endl;
cin >> m >> n;
cout << "Enter the element of matrix A:" << endl;
for (int i = 0; i < m; i++) {
while (cin >> pos && pos != 0 && pos <= n) {
cin >> value;
pos = pos - 1;
list1.addNode(i, pos, value);
}
if (pos > n) {
cout << "It's not a valid column position." << endl;
break;
}
}
cout << "Enter the element of matrix B:" << endl;
for (int j = 0; j < m; j++) {
while (cin >> pos && pos != 0) {
if (pos > n) {
cout << "It's not a valid column position." << endl;
break;
};
cin >> value;
pos = pos - 1;
list2.addNode(j, pos, value);
}
if (pos > n) {
cout << "It's not a valid column position." << endl;
break;
}
}
list3 = list3.matrixAdd(list1, list2);
cout << endl;
cout << "The matrix A:" << endl;
list1.printMatrix(m, n);
cout << endl;
cout << "The matrix B:" << endl;
list2.printMatrix(m, n);
cout << endl;
cout << "The matrix A+B:" << endl;
list3.printMatrix(m, n);
cout << endl;
} else if (operation == 2) {
cout << "Enter the row and column number of A:" << endl;
cin >> m >> n;
cout << endl;
cout << "Enter the column number of B:" << endl;
cin >> l;
cout << endl;
cout << "Enter the element of matrix A:" << endl;
for (int i = 0; i < m; i++) {
while (cin >> pos && pos != 0 && pos <= n) {
cin >> value;
pos = pos - 1;
list1.addNode(i, pos, value);
}
if (pos > n) {
cout << "It's not a valid column position." << endl;
break;
}
}
cout << "Enter the element of matrix B:" << endl;
for (int j = 0; j < n; j++) {
while (cin >> pos && pos != 0) {
if (pos > l) {
cout << "It's not a valid column position." << endl;
break;
};
cin >> value;
pos = pos - 1;
list2.addNode(j, pos, value);
}
if (pos > l) {
cout << "It's not a valid column position." << endl;
break;
}
}
list3 = list3.matrixMultiple(list1, list2, m, n, l);
cout << "The matrix A:" << endl;
list1.printMatrix(m, n);
cout << endl;
cout << "The matrix B:" << endl;
list2.printMatrix(n, l);
cout << endl;
cout << "The matrix AB:" << endl;
list3.printMatrix(m, l);
cout << endl;
}
return 0;
}
void LinkedList::addNode(int newrow_pos, int newcolumn_pos, int newvalue) {
llNode *newNode = new llNode(newvalue);
llNode *temp = new llNode();
llNode *tempNew = new llNode();
if (first == NULL) {
temp->value = newvalue;
temp->row_pos = newrow_pos;
temp->column_pos = newcolumn_pos;
temp->next = NULL;
first = temp;
} else {
temp = first;
while (temp->next != NULL) {
temp = temp->next;
}
tempNew->value = newvalue;
tempNew->row_pos = newrow_pos;
tempNew->column_pos = newcolumn_pos;
tempNew->next = NULL;
temp->next = tempNew;
}
}
void LinkedList::PrintList() {
if (first == NULL) {
cout << "List is empty.\n";
return;
}
llNode *current = first;
while (current != NULL) {
cout << current->value << " ";
current = current->next;
}
cout << endl;
}
void LinkedList::printMatrix(int m, int n) {
llNode *current = first;
int **matrix;
matrix = new int *[m];
for (int i = 0; i < m; i++) {
matrix[i] = new int[m];
for (int j = 0; j < n; j++) {
matrix[i][j] = 0;
}
}
while (current != NULL) {
int row, col;
row = current->row_pos;
col = current->column_pos;
matrix[row][col] = current->value;
current = current->next;
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
LinkedList LinkedList::matrixAdd(LinkedList A, LinkedList B) {
LinkedList C;
llNode *nodeA = A.first;
llNode *nodeB = B.first;
while (nodeA != NULL && nodeB != NULL) {
if (nodeA->row_pos == nodeB->row_pos) {
if (nodeA->column_pos == nodeB->column_pos) {
int sum = nodeA->value + nodeB->value;
C.addNode(nodeA->row_pos, nodeA->column_pos, sum);
nodeA = nodeA->next;
nodeB = nodeB->next;
} else {
if (nodeA->column_pos < nodeB->column_pos) {
C.addNode(nodeA->row_pos, nodeA->column_pos, nodeA->value);
nodeA = nodeA->next;
} else {
C.addNode(nodeB->row_pos, nodeB->column_pos, nodeB->value);
nodeB = nodeB->next;
}
}
} else {
if (nodeA->row_pos < nodeB->row_pos) {
C.addNode(nodeA->row_pos, nodeA->column_pos, nodeA->value);
nodeA = nodeA->next;
} else {
C.addNode(nodeB->row_pos, nodeB->column_pos, nodeB->value);
nodeB = nodeB->next;
}
}
}
while (nodeA != NULL) {
C.addNode(nodeA->row_pos, nodeA->column_pos, nodeA->value);
nodeA = nodeA->next;
}
while (nodeB != NULL) {
C.addNode(nodeB->row_pos, nodeB->column_pos, nodeB->value);
nodeB = nodeB->next;
}
return C;
}
int LinkedList::search(LinkedList A, int row_index, int column_index) {
llNode *start = A.first;
while (start != NULL) {
if (start->row_pos == row_index && start->column_pos == column_index) {
return start->value;
}
start = start->next;
}
return 0;
}
LinkedList LinkedList::matrixMultiple(LinkedList A, LinkedList B, int m, int n,
int l) {
LinkedList C;
int result = 0, valueA = 0, valueB = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < l; j++) {
result = 0;
for (int k = 0; k < n; k++) {
valueA = search(A, i, k);
valueB = search(B, k, j);
result += valueA * valueB;
}
if (result != 0) {
C.addNode(i, j, result);
}
}
}
return C;
}