-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SinglyLinkedList.h
458 lines (385 loc) · 7.5 KB
/
SinglyLinkedList.h
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#pragma once
#include <iostream>
using namespace std;
class SinglyLinkedList
{
public:
struct Node
{
int data = 0;
Node* next = nullptr;
};
private:
Node* head = nullptr;
Node* tail = nullptr;
int count = 0;
public:
SinglyLinkedList()
{
}
~SinglyLinkedList()
{
cout << "DESTRUCTOR!\n";
Clear();
}
void AddToHead(int data)
{
Node* newElem = new Node();
newElem->data = data;
newElem->next = head;
if (head == nullptr)
{
tail = newElem;
}
head = newElem;
count++;
}
void AddToTail(int data)
{
Node* newElem = new Node;
newElem->data = data;
if (head == nullptr) // åñëè ñïèñîê ïóñòîé (â í¸ì 0 ýëåìåíòîâ)
{
head = newElem;
}
else
{
tail->next = newElem;
}
tail = newElem;
count++;
}
void InsertInto(int data, int index)
{
if (index >= count)
{
AddToTail(data);
return;
}
else if (index <= 0)
{
AddToHead(data);
return;
}
// n - óêàçàòåëü íà íîâûé âñòàâëÿåìûé ýëåìåíò ñïèñêà
Node* n = new Node();
n->data = data;
// current - óêàçàòåëü íà òåêóùèé ýëåìåíò ñïèñêà, ïîíà÷àëó - íà íóëåâîé ýëåìåíò ñïèñêà
Node* current = head;
// íåîáõîäèìî âûïîëíèòü index - 1 ïåðåõîäîâ ïî ñïèñêó, äëÿ òîãî ÷òîáû íàéòè ýëåìåíò ÏÅÐÅÄ âñòàâëÿåìûì
for (int i = 1; i < index; i++)
{
current = current->next;
}
// íàñòðîéêà 2 ñâÿçîê:
// ñíà÷àëà íîâîìó ýëåìåíòó ãîâîðèì, ÷òî çà íèì êòî-òî åñòü
n->next = current->next;
// òîìó, êîòîðûé íàõîäèòñÿ ÏÅÐÅÄ âñòàâëÿåìûì - ñîîáùàåì àäðåñ íîâîãî
current->next = n;
count++;
}
void DeleteFromHead()
{
if (count == 0)
{
// cout << "Empty list!\n";
return;
}
Node* temp = head;
head = head->next;
delete temp;
count--;
if (count == 0)
{
tail = nullptr;
}
}
void DeleteFromTail()
{
if (count == 0) {
// cout << "Empty list!\n";
return;
}
DeleteByIndex(count - 1);
}
void DeleteByIndex(int index)
{
if (index <= 0 || count == 1)
{
DeleteFromHead();
return;
}
if (index >= count)
{
index = count - 1;
}
// current - óêàçàòåëü íà òåêóùèé ýëåìåíò ñïèñêà, ïîíà÷àëó - íà íóëåâîé ýëåìåíò ñïèñêà
Node* current = head;
// íåîáõîäèìî âûïîëíèòü index - 1 ïåðåõîäîâ ïî ñïèñêó, äëÿ òîãî ÷òîáû íàéòè ýëåìåíò ÏÅÐÅÄ âñòàâëÿåìûì
for (int i = 1; i < index; i++)
{
current = current->next;
}
// del - óêàçàòåëü íà óäàëÿåìûé ýëåìåíò
Node* del = current->next;
current->next = del->next;
delete del;
count--;
}
void Clear()
{
while (head != nullptr)
{
DeleteFromHead();
}
}
void Print() const
{
if (count == 0)
{
cout << "Empty list!\n";
return;
}
Node* current = head;
while (current != nullptr)
{
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
int GetCount() const
{
return count;
}
int IndexOf(int value) const
{
if (count == 0)
{
return -1;
}
Node* current = head;
for (int i = 0; i < count; i++)
{
if (current->data == value)
{
return i;
}
current = current->next;
}
return -1;
}
bool IsEmpty()
{
return count == 0;
}
void Reverse()
{
if (IsEmpty())
{
return;
}
Node* temp = 0;
Node* current = head;
while (current != NULL)
{
Node* next = current->next;
current->next = temp;
temp = current;
current = next;
}
head = temp;
}
void SortAsc()
{
if (IsEmpty())
{
return;
}
for (Node* i = head; i != NULL; i = i->next)
{
for (Node* j = i->next; j != NULL; j = j->next)
{
if (i->data > j->data)
{
swap(i->data, j->data);
}
}
}
}
void SortDesc()
{
SortAsc();
Reverse();
}
void Shuffle()
{
int k = 0;
int* ar = new int[count];
Node* current = head;
while (current != nullptr)
{
ar[k++] = current->data;
current = current->next;
}
random_shuffle(ar, ar + k);
current = head;
k = 0;
while (current != nullptr)
{
current->data = ar[k++];
current = current->next;
}
delete[] ar;
}
SinglyLinkedList Clone()
{
SinglyLinkedList* copy = this;
return *copy;
}
SinglyLinkedList operator+(const SinglyLinkedList& sll)
{
// ôîðìèðóåì ðåçóëüòèðóþùèé ñïèñîê íà îñíîâàíèè ëåâîãî îïåðàíäà
SinglyLinkedList res = *this;
Node* temp = sll.head;
// ïåðåáèðàåì âñå ýëåìåíòû ïðàâîãî ñïèñêà
for (int i = 0; i < sll.count; i++)
{
// è äîáàâëÿåì èõ â ðåçóëüòèðóþùèé ñïèñîê
res.AddToTail(temp->data);
temp = temp->next;
}
return res;
}
bool operator > (const SinglyLinkedList& sll)
{
return count > sll.count;
}
operator int*()
{
return to_array();
}
int* to_array()
{
int* ar = new int[count];
Node* current = head;
for (int i = 0; i < count; i++)
{
ar[i] = current->data;
current = current->next;
}
return ar;
}
SinglyLinkedList& operator=(const SinglyLinkedList& original)
{
// ïðîâåðêà íà ñàìîïðèñâàèâàíèå
// second = second;
if (this == &original) return *this;
// âñå ýëåìåíòû ñïèñêà-êîïèè äîëæíû áûòü óíè÷òîæåíû!
this->Clear();
// âûñòàâëÿåì óêàçàòåëü íà íà÷àëî îðèãèíàëüíîãî ñïèñêà (êîòîðûé ñåé÷àñ ÿâëÿåòñÿ ïðàâûì îïåðàíäîì â âûðàäåíèè òèïà SLL a = b;
Node* current = original.head;
// ïðîáåãàåìñÿ ïî âñåìó îðèãèíàëüíîìó ñïèñêó îäèí ðàç îò íà÷àëà äî êîíöà
for (int i = 0; i < original.count; i++)
{
// ïðè äîáàâëåíèè â õâîñò êîïèè ñïèñêà ñîçäà¸òñÿ íîâûé íîä
AddToTail(current->data);
// ïåðåìåñòèëèñü íà ñëåäóþùèé ýëåìåíò ñïèñêà-îðèãèíàëà
current = current->next;
}
return *this;
}
SinglyLinkedList(const SinglyLinkedList& original)
{
// âûñòàâëÿåì óêàçàòåëü íà íà÷àëî îðèãèíàëüíîãî ñïèñêà (êîòîðûé ñåé÷àñ ÿâëÿåòñÿ ïðàâûì îïåðàíäîì â âûðàäåíèè òèïà SLL a = b;
Node* current = original.head;
// ïðîáåãàåìñÿ ïî âñåìó îðèãèíàëüíîìó ñïèñêó îäèí ðàç îò íà÷àëà äî êîíöà
for (int i = 0; i < original.count; i++)
{
// ïðè äîáàâëåíèè â õâîñò êîïèè ñïèñêà ñîçäà¸òñÿ íîâûé íîä
AddToTail(current->data);
// ïåðåìåñòèëèñü íà ñëåäóþùèé ýëåìåíò ñïèñêà-îðèãèíàëà
current = current->next;
}
}
Node* ElemAt(int index) const
{
if (index <= 0)
{
return head;
}
if (index >= count - 1)
{
return tail;
}
Node* curent = head;
for (int i = 0; i < index; i++)
{
curent = curent->next;
}
return curent;
}
int& operator[](const int index)
{
if (index < 0 || index >= count) throw "incorrect oops!";
auto counter = 0;
auto* current = head;
while (current)
{
if (counter == index)
return current->data;
current = current->next;
counter++;
}
}
SinglyLinkedList MergeWith(const SinglyLinkedList& sll)
{
return *this + sll;
}
};
// a = b;
// a = b = c = b = a = b = c;
// a++ = a.Clone() = ++c = b++ = a = b = c;
/*
SinglyLinkedList sll;
// âñòàâêà â êîíåö ñïèñêà
sll.AddToTail(10);
sll.AddToTail(20);
sll.AddToTail(30);
sll.AddToTail(40);
sll.Print();
// âñòàâêà â íà÷àëî ñïèñêà
sll.AddToHead(50);
sll.AddToHead(60);
sll.AddToHead(70);
sll.AddToHead(80);
sll.Print();
// êîëè÷åñòâî ýëåìåíòîâ, ïîèñê ýëåìåíòîâ
cout << "count: " << sll.GetCount() << "\n";
cout << "index of 70: " << sll.IndexOf(70) << ", index of 90: " << sll.IndexOf(90) << "\n";
// âñòàâêà ïî ïîçèöèè
sll.InsertInto(-1, -1);
sll.Print();
sll.InsertInto(2, 2);
sll.Print();
sll.InsertInto(22, 22);
sll.Print();
// óäàëåíèå ñ íà÷àëà ñïèñêà
sll.DeleteFromHead();
sll.DeleteFromHead();
sll.DeleteFromHead();
sll.Print();
// óäàëåíèå ñ êîíöà ñïèñêà
sll.DeleteFromTail();
sll.DeleteFromTail();
sll.DeleteFromTail();
sll.Print();
// óäàëåíèå ïî óêàçàííîìó èíäåêñó
sll.DeleteByIndex(0);
sll.DeleteByIndex(1);
sll.DeleteByIndex(2);
sll.Print();
// î÷èñòêà ñïèñêà
sll.Clear();
sll.Print();
*/