-
Notifications
You must be signed in to change notification settings - Fork 1
/
DoublyLinkedList.hpp
executable file
·486 lines (417 loc) · 10.4 KB
/
DoublyLinkedList.hpp
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*---------------------------------------------------------------------------------------------
* Copyright (c) Wilsen Hernandez. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
#ifndef _DOUBLY_LINKED_LIST_HPP_
#define _DOUBLY_LINKED_LIST_HPP_
#include <iostream>
#include "Binode.hpp"
template<class T>
class DoublyLinkedList
{
private:
Binode<T> *_first, *_last;
int _length;
public:
DoublyLinkedList() : _first(NULL), _last(NULL), _length(0) {};
DoublyLinkedList(const DoublyLinkedList&);
~DoublyLinkedList() { clear(); };
bool empty() const { return _length == 0; };
int size() const { return _length; };
bool sorted() const;
T front() const;
T back() const;
void push_front(T);
void push_back(T);
void pop_front();
void pop_back();
void erase(int);
void change(T, int);
void clear();
void reverse();
void sort();
void bubblesort();
void quicksort();
void operator=(const DoublyLinkedList&);
bool operator>(const DoublyLinkedList&) const;
bool operator<(const DoublyLinkedList&) const;
bool operator==(const DoublyLinkedList&) const;
bool operator>=(const DoublyLinkedList&) const;
bool operator<=(const DoublyLinkedList&) const;
template<class Ts>
friend std::ostream& operator<<(std::ostream&, const DoublyLinkedList<Ts> &);
private:
void quicksort(int, int, Binode<T>*, Binode<T>*);
Binode<T>* quicksort_divide(int, int, Binode<T>*, Binode<T>*, int&);
};
template<class T>
DoublyLinkedList<T>::DoublyLinkedList(const DoublyLinkedList& arg)
{
Binode<T> *argpivot = arg._first;
Binode<T> *iprev = NULL;
Binode<T> *add = NULL;
_first = NULL;
_length = arg._length;
while(argpivot)
{
add = new Binode<T>(argpivot->key());
if (_first)
{
add->set_prev(iprev);
iprev->set_next(add);
}
else
_first = add;
iprev = add;
argpivot = argpivot->next();
}
_last = add;
}
template<class T>
bool DoublyLinkedList<T>::sorted() const
{
bool isSorted = true;
if (!empty())
{
Binode<T> *current = _first;
Binode<T> *next = current->next();
int i = 0;
do
{
if (current->key() > next->key())
isSorted = false;
current = next;
next = next->next();
i++;
} while (i < _length && next && isSorted);
}
return isSorted;
}
template<class T>
T DoublyLinkedList<T>::front() const
{
if (_first)
return _first->key();
throw "Empty list";
}
template<class T>
T DoublyLinkedList<T>::back() const
{
if(_last)
return _last->key();
throw "Empty list";
}
template<class T>
void DoublyLinkedList<T>::push_front(T e)
{
Binode<T> *add = new Binode<T>(e);
if(_length == 0)
_last = add;
else
{
add->set_next(_first);
_first->set_prev(add);
}
_first = add;
_length++;
}
template<class T>
void DoublyLinkedList<T>::push_back(T e)
{
Binode<T> *add = new Binode<T>(e);
if(_length == 0)
_first = add;
else
{
add->set_prev(_last);
_last->set_next(add);
}
_last = add;
_length++;
}
template<class T>
void DoublyLinkedList<T>::pop_front()
{
if (!empty())
{
Binode<T> *aux = _first;
_first = aux->next();
_first->set_prev(NULL);
_length--;
delete aux;
}
else
throw "Empty list";
}
template<class T>
void DoublyLinkedList<T>::pop_back()
{
if (!empty())
{
Binode<T> *aux = _last;
_last = aux->prev();
_last->set_next(NULL);
_length--;
delete aux;
}
else
throw "Empty list";
}
template<class T>
void DoublyLinkedList<T>::erase(int index)
{
if (empty())
throw "Empty list";
else if (index < 1 || index > _length + 1)
throw "Index out of bounds";
else
{
Binode<T> *aux;
if (index == 1)
{
aux = _first;
_first = aux->next();
_first->set_prev(NULL);
}
else if (index == _length)
{
aux = _last;
_last = aux->prev();
_last->set_next(NULL);
}
else
{
Binode<T> *pivot = _first;
for (int i = 1; i < index; i++)
pivot = pivot->next();
aux = pivot;
aux->next()->set_prev(aux->prev());
aux->prev()->set_next(aux->next());
}
delete aux;
_length--;
}
}
template<class T>
void DoublyLinkedList<T>::change(T e, int index)
{
if (empty())
throw "Empty list";
else if (index < 1 || index > _length + 1)
throw "Index out of bounds";
else
{
if (index == 1)
_first->set_key(e);
else if (index == _length)
_last->set_key(e);
else
{
Binode<T> *pivot = _first;
for (int i = 1; i < index; i++)
pivot = pivot->next();
pivot->set_key(e);
}
}
}
template<class T>
void DoublyLinkedList<T>::clear()
{
Binode<T> *pivot = _first;
Binode<T> *aux;
while (pivot)
{
aux = pivot;
pivot = pivot->next();
delete aux;
}
_first = NULL;
_last = NULL;
_length = 0;
}
template<class T>
void DoublyLinkedList<T>::reverse()
{
Binode<T> *current = _first;
Binode<T> *temp = NULL;
if (current)
_last = _first;
while (current)
{
temp = current->prev();
current->set_prev(current->next());
current->set_next(temp);
current = current->prev();
}
if (temp)
_first = temp->prev();
}
template<class T>
void DoublyLinkedList<T>::sort()
{
if (_length <= 20)
this->bubblesort();
else
this->quicksort();
}
template<class T>
void DoublyLinkedList<T>::bubblesort()
{
if (!sorted())
{
for(int p = 0, n = _length; p < n - 1; p++)
{
Binode<T> *pivot = _first;
Binode<T> *next = pivot->next();
for (int j = 0; j < n - p - 1; j++)
{
if (pivot->key() > next->key())
Binode<T>::swap(pivot, next);
pivot = next;
next = next->next();
}
}
}
}
template<class T>
void DoublyLinkedList<T>::quicksort()
{
if (!sorted())
{
quicksort(1, _length, _first, _last);
}
}
template<class T>
void DoublyLinkedList<T>::quicksort(int start, int end, Binode<T>* nStart, Binode<T>* nEnd)
{
int pivot;
Binode<T> *nPivot = NULL;
if (start < end)
{
nPivot = quicksort_divide(start, end, nStart, nEnd, pivot);
// Recursive calls from here
quicksort(start, pivot - 1, nStart, nPivot->prev());
quicksort(pivot + 1, end, nPivot->next(), nEnd);
}
}
template<class T>
Binode<T>* DoublyLinkedList<T>::quicksort_divide(int start, int end, Binode<T>* nStart, Binode<T>* nEnd, int& pivot)
{
int left, right;
Binode<T> *nLeft, *nRight;
T ppivot = nStart->key();
left = start;
right = end;
nLeft = nStart;
nRight = nEnd;
while (left < right)
{
while (nRight->key() > ppivot)
{
nRight = nRight->prev();
right--;
}
while ((left < right) && (nLeft->key() <= ppivot))
{
nLeft = nLeft->next();
left++;
}
if (left < right)
Binode<T>::swap(nLeft, nRight);
}
Binode<T>::swap(nRight, nStart);
pivot = right;
return nRight;
}
template<class T>
void DoublyLinkedList<T>::operator=(const DoublyLinkedList<T> &list)
{
if (this != &list)
{
this->clear();
Binode<T> *listPivot = list._first;
Binode<T> *thisPivot = this->_first;
Binode<T> *nuevo;
for (int i = 1; i <= list._length; i++, listPivot = listPivot->next())
{
nuevo = new Binode<T>();
nuevo->set_key(listPivot->key());
if (i == 1)
{
this->_first = nuevo;
thisPivot = nuevo;
}
else
{
thisPivot->set_next(nuevo);
nuevo->set_prev(thisPivot);
thisPivot = thisPivot->next();
}
}
_last = nuevo;
this->_length = list._length;
}
}
template<class T>
bool DoublyLinkedList<T>::operator>(const DoublyLinkedList<T> &v) const
{
if (this != &v)
return (this->_length > v._length);
return false;
}
template<class T>
bool DoublyLinkedList<T>::operator<(const DoublyLinkedList<T> &v) const
{
return !(*this > v);
}
template<class T>
bool DoublyLinkedList<T>::operator==(const DoublyLinkedList<T> &v) const
{
if (this != &v)
{
if (this->_length == v._length)
{
Binode<T> *thisPivot = this->_first;
Binode<T> *vPivot = v._first;
bool isEqual;
int i = 1;
do
{
isEqual = (thisPivot->key() == vPivot->key());
i++;
thisPivot = thisPivot->next();
vPivot = vPivot->next();
} while(i <= this->_length && isEqual);
return isEqual;
}
return false;
}
return true;
}
template<class T>
bool DoublyLinkedList<T>::operator>=(const DoublyLinkedList<T> &v) const
{
if (this != &v)
return (*this > v || *this == v);
return true;
}
template<class T>
bool DoublyLinkedList<T>::operator<=(const DoublyLinkedList<T> &v) const
{
if (this != &v)
return (*this < v || *this == v);
return true;
}
template<class T>
std::ostream& operator<<(std::ostream& out, const DoublyLinkedList<T> &list)
{
Binode<T> *Node;
Node = list._first;
for (int i = 0; i < list._length; i++, Node = Node->next())
if (Node != list._last)
out << Node->key() << " ";
else
out << Node->key();
return out;
}
#endif