-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.cpp
281 lines (246 loc) · 5.9 KB
/
List.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
/*
* DataStructure - List
* class name: List
*
* properties:
* - length
* - firstItem
* - lastItem
*
* methods:
* - iterator
* - push (left, right)
* - pop (left, right)
* - insert
* - remove
* - reverse
* - merge
* - slice
* - clear
* - size
*
* common methods:
* - set
* - get
*/
#include <iostream>
template <typename T>
struct ListItem {
T value;
ListItem<T>* pevItem = nullptr;
ListItem<T>* nextItem = nullptr;
void link(ListItem<T>* PevItem, ListItem<T>* NextItem) {
pevItem = PevItem;
nextItem = NextItem;
}
ListItem<T>* reverseLink() {
ListItem<T>* tmp = nextItem;
nextItem = pevItem;
pevItem = tmp;
return tmp;
}
};
template <typename T>
class List {
public:
ListItem<T>* firstItem = nullptr;
ListItem<T>* lastItem = nullptr;
const int& length = _length;
void clear() {
if (_length == 0) return;
ListItem<T>* item = firstItem;
ListItem<T>* item2 = firstItem;
for (int i = 0; i < _length; i++) {
item2 = item;
item = item->nextItem;
delete item2;
}
firstItem = lastItem = nullptr;
_length = 0;
}
List() {
firstItem = lastItem = nullptr;
_length = 0;
}
~List() {
clear();
}
List(std::initializer_list<T> l) {
for (auto it = l.begin(); it != l.end(); ++it) {
push(*it);
}
}
List(const List<T>& l) {
ListItem<T>* item = l.firstItem;
for (int i = 0; i < l.length; i++) {
push(item->value);
item = item->nextItem;
}
}
ListItem<T>& iterator(int index) {
setlen(index);
ListItem<T>* item;
if (_length - index > index) {
item = firstItem;
for (int i = 0; i < index; i++) item = item->nextItem;
} else {
item = lastItem;
for (int i = 0; i < _length - index - 1; i++) item = item->pevItem;
}
return *item;
}
void pushRight(T value) {
ListItem<T>* item = newItem(value);
++_length;
if (_length == 1) {
firstItem = lastItem = item;
} else {
item->pevItem = lastItem;
lastItem->nextItem = item;
lastItem = item;
}
}
void pushLeft(T value) {
ListItem<T>* item = newItem(value);
++_length;
if (_length == 1) {
firstItem = lastItem = item;
} else {
item->nextItem = firstItem;
firstItem->pevItem = item;
firstItem = item;
}
}
T popRight() {
--_length;
if (length == 0) {
T value = lastItem->value;
clear();
return value;
}
ListItem<T>* item = lastItem->pevItem;
T value = lastItem->value;
delete lastItem;
item->nextItem = nullptr;
lastItem = item;
return value;
}
T popLeft() {
--_length;
if (length == 0) {
T value = firstItem->value;
clear();
return value;
}
ListItem<T>* item = firstItem->nextItem;
T value = firstItem->value;
delete firstItem;
item->pevItem = nullptr;
firstItem = item;
return value;
}
void insert(int index, T value) {
setlen(index);
if (index == 0)
pushLeft(value);
else if (index == _length - 1)
pushRight(value);
else {
ListItem<T>* thisItem = newItem(value);
ListItem<T>& item1 = iterator(index-1);
ListItem<T>& item2 = iterator(index);
thisItem->link(&item1, &item2);
item1.nextItem = thisItem;
item2.pevItem = thisItem;
}
}
T remove(int index) {
setlen(index);
if (index == 0)
return popLeft();
else if (index == _length - 1)
return popRight();
else {
ListItem<T>* item = &iterator(index);
item->pevItem->nextItem = item->nextItem;
item->nextItem->pevItem = item->pevItem;
T value = item->value;
delete item;
--_length;
return value;
}
}
void reverse() {
ListItem<T>* thisItem = firstItem;
for (int i = 0; i < _length; i++) {
thisItem = thisItem->reverseLink();
}
ListItem<T>* tmp = firstItem;
firstItem = lastItem;
lastItem = tmp;
}
List<T> merge(List<T> x) {
List<T> l = *this;
ListItem<T>* item = x.firstItem;
for (int i = 0; i < x.length; i++) {
l.push(item->value);
item = item->nextItem;
}
return l;
}
List<T> slice(int startIndex, int endIndex = -1) {
setlen(startIndex); setlen(endIndex);
ListItem<T>* item = &iterator(startIndex);
List<T> l;
for (int i = startIndex; i <= endIndex; i++) {
l.push(item->value);
item = item->nextItem;
}
return l;
}
void set(int index, T value) {
iterator(index).value = value;
}
T& get(int index) {
return iterator(index).value;
}
// sizeof full object
unsigned int size() {
return sizeof(*this) + _length * sizeof(*firstItem);
}
// alias
inline void push(T value) { pushRight(value); }
inline T pop() { return popRight(); }
inline T& operator[] (int index) {
return get(index);
}
inline List<T> operator+ (List<T> x) {
return merge(x);
}
List<T>& operator= (std::initializer_list<T> l) {
for (auto it = l.begin(); it != l.end(); ++it) {
push(*it);
}
}
friend std::ostream& operator<<(std::ostream& out, List<T> const& list) {
out << '[';
ListItem<T>* item = list.firstItem;
for (int i = 0; i < list._length; i++) {
out << item->value;
if (i + 1 != list._length) out << ", ";
item = item->nextItem;
}
out << ']';
return out;
}
private:
int _length = 0;
inline void setlen(int& x) {
if (x < 0) x += _length;
}
ListItem<T>* newItem(T value) {
ListItem<T>* item = new ListItem<T>;
item->value = value;
return item;
}
};