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
| #include "stdafx.h"
#include "CppUnitTest.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
class Element
{
private:
int value;
int row;
int col;
Element* down;
Element* right;
public:
Element()
:value(0), row(0),
col(0), down(nullptr),
right(nullptr)
{
}
Element(Element & a)
: value(a.value),
row(a.row), col(a.col),
down(a.down),
right(a.right)
{
}
Element(int avalue, int arow, int arod)
: value(avalue),
row(arow),
col(arod),
down(nullptr),
right(nullptr)
{
}
int getValue()
{
return value;
}
int getRow()
{
return row;
}
int getCol()
{
return col;
}
Element* getDown()
{
return down;
}
Element *getRight()
{
return right;
}
void setValue(int a)
{
value = a;
}
void setRow(int a)
{
row = a;
}
void setCol(int a)
{
col = a;
}
void setDown(Element* a)
{
down = a;
}
void setRight(Element*a)
{
right = a;
}
};
class Matrix
{
protected:
Element* col;
Element* row;
int size_rows;
int size_cols;
public:
Matrix() :
col(nullptr),
row(nullptr),
size_rows(0),
size_cols(0)
{
}
Matrix(const int& size_cols1, const int& size_rows1) :
size_cols(size_cols1),
size_rows(size_rows1)
{
col = new Element[size_cols];
row = new Element[size_rows];
for (int i = 0; i < size_cols; ++i)
{
col[i].setCol(i);
col[i].setRow(-1);
}
for (int i = 0; i < size_rows; ++i)
{
row[i].setRow(i);
row[i].setCol(-1);
}
}
Matrix(Matrix & a) :
size_rows(a.size_rows),
size_cols(a.size_cols)
{
col = new Element[size_cols];
row = new Element[size_rows];
for (int i = 0; i < size_cols; ++i)
{
col[i].setCol(i);
col[i].setRow(-1);
}
for (int i = 0; i < size_rows; ++i)
{
row[i].setRow(i);
row[i].setCol(-1);
}
for (int i = 0; i < size_rows; i++)
{
Element ptr = a.row[i];
while (ptr.getRight())
{
ptr = *ptr.getRight();
push(ptr.getValue(), i, ptr.getCol());
}
}
}
void push(int value, int row, int col)
{
if (value == 0)
{
return;
}
Element* elementToPush = new Element(value, row, col);
Element* first = &this->row[row];
Element* first1 = first;
while (first->getCol() < col && first->getRight() != nullptr)
{
first1 = first;
first = first->getRight();
}
if (first->getCol() > col)
{
elementToPush->setRight(first1->getRight());
first1->setRight(elementToPush);
}
else if (first->getCol() == col)
{
first->setValue(value);
}
else if (first->getRight() == nullptr)
{
first->setRight(elementToPush);
}
first = &this->col[col];
first1 = first;
while (first->getRow() < row&& first->getDown())
{
first1 = first;
first = first->getDown();
}
if (first->getRow() > row)
{
elementToPush->setDown(first1->getDown());
first1->setDown(elementToPush);
}
else if (!first->getDown())
{
first->setDown(elementToPush);
}
}
~Matrix()
{
for (int i = 0; i < size_rows; i++)
{
Element* pos = &(row[i]);
Element* ptrToMove = pos->getRight();
if (ptrToMove != nullptr)
{
while (ptrToMove->getRight() != nullptr)
{
Element* ptrToDel = ptrToMove;
ptrToMove = ptrToMove->getRight();
delete ptrToDel;
}
delete ptrToMove;
}
}
delete[] col;
delete[] row;
} |