-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.h
More file actions
330 lines (310 loc) · 9.94 KB
/
LinkedList.h
File metadata and controls
330 lines (310 loc) · 9.94 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#ifndef LinkedList_h
#define LinkedList_h
#include <iostream>
// Struct representing sparse matrix entry data and pointers referring
// to relative location withing the LinkedList
struct linkedNode {
int row;
int col;
float val;
linkedNode *next;
linkedNode *previous;
};
class LinkedList {
private:
int count; // Number of entries in linkedList. Accessed via length()
int maxRow;
int maxCol;
linkedNode *header; // Pointer where header->next is first entry
linkedNode *footer; // Pointer where footer->next = NULL and last entry
public:
LinkedList();
~LinkedList();
int length();
void setDimensions(int, int);
int getRows();
int getCols();
linkedNode* getHeader();
bool isCompatibleWith(LinkedList&);
bool isSorted();
void sort();
void append(linkedNode*);
void loopSum(LinkedList&, LinkedList&);
void recSum(linkedNode*&, linkedNode*&, LinkedList&);
void writeToFile(std::string);
void print();
};
// Allocates header in memory and points to footer (must be free'd)
// Initiliazes all members to default values.
LinkedList::LinkedList() {
header = new linkedNode;
header->next = footer;
header->previous = NULL;
footer = NULL;
count = 0;
maxRow = 0;
maxCol = 0;
}
// If non-empty LinkedList, delete all entries allocated using new.
// Otherwise, header is the only allocated block with new that needs handling.
LinkedList::~LinkedList() {
linkedNode *current;
if(count > 0) {
while(header != NULL) {
current = header;
header = header->next;
delete current;
}
footer = NULL;
delete header;
header = NULL;
}
else {
delete header;
header = NULL;
}
}
int LinkedList::length() {
return count;
}
void LinkedList::setDimensions(int row, int col) {
maxRow = row;
maxCol = col;
}
int LinkedList::getRows() {
return maxRow;
}
int LinkedList::getCols() {
return maxCol;
}
linkedNode* LinkedList::getHeader() {
return header;
}
/**
* Verify if LinkedLists are compatible for sparse matrix addition
* @param - LinkedList to be check against this
* @return True if rows and columns of this and other are equal
*/
bool LinkedList::isCompatibleWith(LinkedList &other) {
return (this->maxRow == other.getRows() && this->maxCol == other.getCols());
}
/**
* Check if LinkedList is currently sorted
* @return - True if node n is strictly less than or equal to node n+1
*/
bool LinkedList::isSorted() {
if(count == 0)
return true;
linkedNode *current = header;
linkedNode *next = current->next;
while(current->next) {
if(current->row > next->row) {
return false;
}
else if(current->row == next->row || current->col > next->col) {
return false;
}
current = current->next;
next = current->next;
}
std::cout << "true\n";
return true;
}
/**
* Sorts LinkedList in ascending order if 'this' is found to unsorted via
* bubble sort and value swaps
*/
void LinkedList::sort() {
linkedNode *current = header->next;
linkedNode *next = current->next;
for(int i = 0; i < count; i++) {
while(current->next != NULL){
if((current->row > next->row) || (current->row == next->row && current->col > next->col)) {
int row = current->row;
int col = current->col;
float val = current->val;
current->row= next->row;
current->col = next->col;
current->val = next->val;
next->row = row;
next->col = col;
next->val = val;
}
current = current->next;
next = next->next;
}
current = header->next;
next = current->next;
}
current = NULL;
next = NULL;
}
/**
* Adds new linkedNode to end of LinkedList
* @param - pointer to linkedNode to be added to LinkedList
*/
void LinkedList::append(linkedNode *e) {
linkedNode *entry = e;
if(footer == NULL) {
header->next = entry;
footer = entry;
footer->next = NULL;
footer->previous = header;
count++;
}
else {
footer->next = entry;
entry->previous = footer;
entry->next = NULL;
footer = entry;
count++;
}
entry = NULL;
}
/**
* Matrix addition of this LinkedList and some LinkedList B using loops
* @param bList - LinkedList to be added with this LinkedList
* @param finalList - LinkedList to contain the results of A+B
*/
void LinkedList::loopSum(LinkedList &bList, LinkedList &finalList) {
finalList.setDimensions(this->maxRow, this->maxCol);
linkedNode *current_a = NULL;
linkedNode *current_b = NULL;
if(count > 0)
current_a = header->next;
if(bList.length() > 0)
current_b = (bList.getHeader())->next;
printf("a count: %d, b count: %d\n", count, bList.length());
while(current_a && current_b) {
if(current_a->row == current_b->row && current_a->col == current_b->col) {
float val = current_a->val + current_b->val;
if(val != 0.0) {
linkedNode *temp = new linkedNode;
temp->row = current_a->row;
temp->col = current_b->col;
temp->val = val;
finalList.append(temp);
}
current_a = current_a->next;
current_b = current_b->next;
}
else if(current_a->row < current_b->row || (current_a->row == current_b->row && current_a->col < current_b->col)) {
linkedNode *entry = new linkedNode;
*entry = *current_a;
finalList.append(entry);
current_a = current_a->next;
}
else if(current_b->row < current_a->row || (current_b->row == current_a->row && current_b->col < current_a->col)) {
linkedNode *entry = new linkedNode;
*entry = *current_b;
finalList.append(entry);
current_b = current_b->next;
}
}
while(current_a) {
linkedNode *entry = new linkedNode;
*entry = *current_a;
finalList.append(entry);
current_a = current_a->next;
}
while(current_b) {
linkedNode *entry = new linkedNode;
*entry = *current_b;
finalList.append(entry);
current_b = current_b->next;
}
}
/**
* Matrix addition of this LinkedList and some LinkedList B using recursion
* @param bList - LinkedList to be added with this LinkedList
* @param finalList - LinkedList to contain the results of A+B
*/
void LinkedList::recSum(linkedNode *&a, linkedNode *&b, LinkedList &finalList) {
std::cout << "rec sum\n";
linkedNode *current_a = a;
linkedNode *current_b = b;
if(current_a && current_b) {
float val = current_a->val + current_b->val;
if(current_a->row == current_b->row && current_a->col == current_b->col) {
if(val != 0.0) {
linkedNode *temp = new linkedNode;
temp->val = val;
temp->row = current_a->row;
temp->col = current_a->col;
finalList.append(temp);
current_a = current_a->next;
current_b = current_b->next;
recSum(current_a, current_b, finalList);
}
}
else if(current_a->row < current_b->row || (current_a->row == current_b->row && current_a->col < current_b->col)) {
linkedNode *entry = new linkedNode;
*entry = *current_a;
finalList.append(entry);
current_a = current_a->next;
recSum(current_a, current_b, finalList);
}
else if(current_b->row < current_a->row || (current_b->row == current_a->row && current_b->col < current_a->col)) {
linkedNode *entry = new linkedNode;
*entry = *current_b;
finalList.append(entry);
current_b = current_b->next;
recSum(current_a, current_b, finalList);
}
}
else if(current_a) {
linkedNode *temp = new linkedNode;
temp->val = current_a->val;
temp->row = current_a->row;
temp->col = current_a->col;
finalList.append(temp);
current_a = current_a->next;
recSum(current_a, current_b, finalList);
}
else if(current_b) {
linkedNode *temp = new linkedNode;
temp->val = current_b->val;
temp->row = current_b->row;
temp->col = current_b->col;
finalList.append(temp);
current_b = current_b->next;
recSum(current_a, current_b, finalList);
}
}
/**
* Writes contents of this LinkedList to specified file name
* @param - String of file name to be written into
*/
void LinkedList::writeToFile(std::string out) {
FILE *outputWriter;
outputWriter = fopen(out.c_str(), "w");
if(outputWriter) {
fprintf(outputWriter, "%s", "#Matrix C=A+B\n");
//fprintf(outputWriter, "%s", "#### SPARSE MATRIX ####\n");
linkedNode *current = header->next;
for(int i = 0; i < count; i++) {
std::cout << "writing\n";
fprintf(outputWriter, "%d %d %.2f\n", current->row, current->col, current->val);
current = current->next;
}
//printf("Dimensions: %dx%d\n", maxRow, maxCol);
if(count == 0) {
fprintf(outputWriter, "%d %d 0.00\n", maxRow, maxCol);
return;
}
if((footer->row != maxRow) || (footer->col != maxCol)) {
fprintf(outputWriter, "%d %d 0.00\n", maxRow, maxCol);
}
}
else {
std::cout << "Failed to open <" << out << ">for file write\n";
}
}
void LinkedList::print() {
linkedNode *current = header->next;
while(current != NULL) {
printf("Row: %d | Col: %d | Val: %.2f\n", current->row, current->col, current->val);
current = current->next;
}
}
#endif