-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparseMatrix.c
More file actions
235 lines (213 loc) · 6.24 KB
/
SparseMatrix.c
File metadata and controls
235 lines (213 loc) · 6.24 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
#include "SparseMatrix.h"
SparseMatrix* FindAboveSparseMatrix(int row, int col, SparseMatrix* node)
{
SparseMatrix* current = node->nextRow;
while (current->col != col && current != node)
{
current = current->nextRow;
}
if (current->col == col)
if (current->row != current->nextRow->row)
while (current->row != row && current != node)
{
current = current->nextCol;
}
else {
current == NULL;
}
return current;
}
SparseMatrix* FindBeforeSparseMatrix(int row, int col,SparseMatrix* node)
{
SparseMatrix* current = node->nextCol;
while (current->row != row && current != node)
{
current = current->nextCol;
}
if(current->row == row);
if (current->col != current->nextCol->col)
while (current->col != col && current != node)
{
current = current->nextRow;
}
else
{
current = NULL;
}
return current;
}
void InsertAfterSparseMatrix(int row, int col, SparseMatrix* node, void* value)
{
BOOL isValid = IsEmptyCellSparseMatrix(row, col, node);
SparseMatrix* above = FindAboveSparseMatrix(row, col, node);
SparseMatrix* before = find_before_sparse_matrix(row, col, node);
SparseMatrix* newNode = (SparseMatrix*)malloc(sizeof(SparseMatrix));
if (above && before && isValid) {
newNode->info = value;
newNode->row = row;
newNode->col = col;
newNode->nextRow = above->nextRow;
newNode->nextCol = before->nextCol;
before->nextRow = newNode;
above->nextCol = newNode;
}
}
BOOL IsEmptyCellSparseMatrix(int row, int col, SparseMatrix* node)
{
SparseMatrix* current = FindBeforeSparseMatrix(row, col, node);
return node == NULL || !current || current->nextCol->row != row || current->nextCol->col != col;
}
void DeleteAfterSparseMatrix(int row, int col, SparseMatrix* node)
{
BOOL isValid = !IsEmptyCellSparseMatrix(row, col, node);
SparseMatrix* above = FindAboveSparseMatrix(row, col, node);
SparseMatrix* before = FindBeforeSparseMatrix(row, col, node);
SparseMatrix* toDelete = before->nextCol;
if (above && before && isValid) {
before->nextCol = toDelete->nextCol;
above->nextRow = toDelete->nextRow;
free(toDelete);
}
}
void InitSparseMatrix(int row, int col, SparseMatrix** manger)
{
if (row > 1 && col > 1) {
int count;
SparseMatrix* current;
SparseMatrix* next;
SparseMatrix* RowAndColManger = (SparseMatrix*)malloc(sizeof(SparseMatrix));
*manger = RowAndColManger;
RowAndColManger->info = '\0';
RowAndColManger->col = 0;
RowAndColManger->row = 0;
RowAndColManger->nextCol = (SparseMatrix*)malloc(sizeof(SparseMatrix));
(RowAndColManger->nextCol)->col = 0;
(RowAndColManger->nextCol)->row = 1;
(RowAndColManger->nextCol)->info = '\0';
RowAndColManger->nextRow = (SparseMatrix*)malloc(sizeof(SparseMatrix));
(RowAndColManger->nextRow)->col = 1;
(RowAndColManger->nextRow)->row = 0;
(RowAndColManger->nextRow)->info = '\0';
current = RowAndColManger->nextCol;
for (count = 0; count < row - 1; count++)
{
next = (SparseMatrix*)malloc(sizeof(SparseMatrix));
next->info = '\0';
next->row = count + 2;
next->col = 0;
current->nextCol = next;
current->nextRow = current;
current = next;
}
current->nextCol = RowAndColManger;
current->nextRow = current;
current = RowAndColManger->nextRow;
for (count = 0; count < col - 1; count++)
{
next = (SparseMatrix*)malloc(sizeof(SparseMatrix));
next->info = '\0';
next->col = count + 2;
next->row = 0;
current->nextRow = next;
current->nextCol = current;
current = next;
}
current->nextRow = RowAndColManger;
current->nextCol = current;
}
}
BOOL IsEmptySparseMatrix(SparseMatrix* manger)
{
BOOL res = (!manger) || ((SparseMatrix*)manger)->nextCol != manger ||
(( SparseMatrix*)manger)->nextRow != manger;
SparseMatrix* prog = ((SparseMatrix*)manger)->nextCol;
while (!res && prog != manger)
{
res = prog->nextRow != prog;
prog = prog->nextCol;
}
prog = ((SparseMatrix*)manger)->nextRow;
while (!res && prog != manger)
{
res = prog->nextCol != prog;
prog = prog->nextRow;
}
return !res;
}
void AddColSparseMatrix(SparseMatrix** manger) {
SparseMatrix* current = *manger;
SparseMatrix* next;
SparseMatrix* col_head = current->nextCol;
int col_num = current->nextCol->col; // The current number of columns
// Iterate to the last column
while (current->nextCol != col_head) {
current = current->nextCol;
}
next = (SparseMatrix*)malloc(sizeof(SparseMatrix));
next->info = '\0';
next->col = col_num + 1;
next->row = 0;
current->nextCol = next;
next->nextCol = col_head;
// Add the new column to the rows
current = col_head;
while (current->nextRow != current) {
next = (SparseMatrix*)malloc(sizeof(SparseMatrix));
next->info = '\0';
next->col = col_num + 1;
next->row = current->nextRow->row;
current->nextRow->nextCol = next;
next->nextCol = current->nextRow;
current = current->nextRow;
}
}
void AddRowSparseMatrix(SparseMatrix** manager)
{
int row = (*manager)->nextCol->row;
int col = (*manager)->nextRow->col;
SparseMatrix* current = (*manager)->nextCol;
while (current->nextCol != *manager)
{
current = current->nextCol;
}
SparseMatrix* newRowStart = (SparseMatrix*)malloc(sizeof(SparseMatrix));
newRowStart->row = row;
newRowStart->col = 0;
newRowStart->nextRow = newRowStart;
newRowStart->nextCol = *manager;
current->nextCol = newRowStart;
SparseMatrix* currentCol = (*manager)->nextRow;
SparseMatrix* currentRow = newRowStart;
for (int i = 0; i < col; i++)
{
SparseMatrix* newNode = (SparseMatrix*)malloc(sizeof(SparseMatrix));
newNode->row = row;
newNode->col = i + 1;
newNode->info = '\0';
currentCol->nextRow = newNode;
currentRow->nextCol = newNode;
currentCol = currentCol->nextCol;
currentRow = newNode;
}
currentRow->nextCol = newRowStart;
currentCol->nextRow = currentRow;
}
void PrintSparseMatrixNode(SparseMatrix* node)
{
BOOL res = IsEmptySparseMatrix(node);
if (!res) {
SparseMatrix* ptr = node->nextCol;
SparseMatrix* temp;
while (ptr->row)
{
temp = ptr->nextRow;
while (temp->col)
{
printf("( %d ) ", temp->info);
temp = temp->nextRow;
}
printf("\n");
ptr = ptr->nextCol;
}
}
}