-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.h
1203 lines (1018 loc) · 20.2 KB
/
list.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef DOUBLY_LINKED_LIST_HEADER
#define DOUBLY_LINKED_LIST_HEADER
#include <iostream>
#include <fstream>
using namespace std;
template<typename T>
class list {
private:
class Node {
public:
Node* prev;
Node* next;
T data;
Node(Node* prev, Node* next, T data) {
this->prev = prev;
this->next = next;
this->data = data;
}
};
Node* head;
Node* tail;
int elements;
Node* position(int i) //returns a pointer to the i-th element
{
Node* p = head;
while (i > 0)
{
p = p->next;
--i;
}
return p;
}
Node* rposition(int i) //returns a pointer to the i-th element (the list is numbered from end to start)
{
Node* p = tail;
while (i > 0)
{
p = p->prev;
--i;
}
return p;
}
void swap_pos(T& x, T& y) //swaps two elements
{ //it's called by erase, rerase functions
T m = x;
x = y;
y = m;
}
void destroy(Node* p, Node* q) //frees the memory from the p-th to q-th memory address
{ //it's called by erase, rerase functions
Node* a;
while (p != q)
{
a = p;
p = p->next;
delete a;
}
delete p;
}
int split(int left, int right) //splitting used by quick_sort() function
{
Node* p = position(left);
Node* q = p;
T pivot = value(right);
T data;
int i = left;
for (int j = left; j < right; ++j)
{
if (q->data <= pivot)
{
data = p->data;
p->data = q->data;
q->data = data;
p = p->next;
++i;
}
q = q->next;
}
data = p->data;
p->data = q->data;
q->data = data;
return i;
}
void quick_sort(int left, int right) //sorts in ascending order
{
if (left < right)
{
int m = split(left, right);
quick_sort(left, m - 1);
quick_sort(m + 1, right);
}
}
int rsplit(int left, int right) //splitting used by quick_rsort() function
{
Node* p = position(left);
Node* q = p;
T pivot = value(right);
T data;
int i = left;
for (int j = left; j < right; ++j)
{
if (q->data >= pivot)
{
data = p->data;
p->data = q->data;
q->data = data;
p = p->next;
++i;
}
q = q->next;
}
data = p->data;
p->data = q->data;
q->data = data;
return i;
}
void quick_rsort(int left, int right) //sorts in descending order
{
if (left < right)
{
int m = rsplit(left, right);
quick_rsort(left, m - 1);
quick_rsort(m + 1, right);
}
}
public:
list(); //default constructor
list(int, T x = 0); //initializer constructor
~list(); //destructor
void push_front(T); //adds a new element at the beginning of the list
void push_back(T); //adds a new element at the end of the list
void pop_front(); //removes the first element of the list
void pop_back(); //removes the last element of the list
T front(); //returns the first element of the list
T back(); //returns the last element of the list
T value(int); //returns the i-th element
T rvalue(int); //returns the i-th element from the backward numbered list
T min(); //returns the smallest element of the list
T max(); //returns the largest element of the list
void clear(); //removes all the elements from the list
bool empty(); //checks if the list is empty (returns a boolean value)
int size(); //returns the number of elements in the list
void swap(int, int); //swaps 2 elements at the given positions
void rswap(int, int); //swaps 2 elements at the given positions from the backward numbered list
void replace(T, T); //replaces all elements to the 2nd parameter that are equal to the first parameter
void remove(T); //deletes all elements equal to the given parameter
void reverse(); //reverses the list
void resize(int); //resizes the list
void rresize(int); //resizes the list (numbered from backwards)
void resize(int, T); //resizes the list and initializes the new elements with the given 2nd parameter
bool contains(T); //checks if the given element is in the list
void sort(); //sorts the list in ascending order
void rsort(); //sorts the list in descending order
bool ascending(); //checks if the elements in the list are in ascending order
bool descending(); //checks if the elements in the list are in descending order
void unique(); //transforms the sorted list to a set
void insert_before(int, T); //inserts an element before the i-th position
void insert_after(int, T); //inserts an element after the i-th position
void rinsert_before(int, T); //inserts an element before the i-th position (numbered from backwards)
void rinsert_after(int, T); //inserts an element after the i-th position (numbered from backwards)
void insert_before(int, int, T); //inserts N element before the i-th position
void insert_after(int, int, T); //inserts N element after the i-th position
void rinsert_before(int, int, T); //inserts N element before the i-th position (numbered from backwards)
void rinsert_after(int, int, T); //inserts N element after the i-th position (numbered from backwards)
void erase(int); //removes the element on the i-th position
void erase(int, int); //removes the elements between the i- and j-th positions
void rerase(int); //removes the element on the i-th position (numbered from backwards)
void rerase(int, int); //removes the elements between the i- and j-th positions (numbered from backwards)
void read(); //reads the list from console
void read(const char*); //reads the list from file
void print(); //prints the list to console
void print(const char*); //prints the list to file
void rprint(); //prints the list to console in reversed order
void rprint(const char*); //prints the list to file in reversed order
};
template<typename T>
list<T>::list()
{
head = nullptr;
tail = nullptr;
elements = 0;
}
template<typename T>
list<T>::list(int n, T data)
{
elements = n;
Node* p = new Node(nullptr, nullptr, data);
head = p;
tail = p;
for (int i = 1; i < elements; ++i)
{
p = new Node(tail, nullptr, data);
p->prev->next = p;
tail = p;
}
}
template<typename T>
list<T>::~list()
{
Node* p;
while (tail != nullptr)
{
p = tail;
tail = tail->prev;
delete p;
}
head = nullptr;
elements = 0;
}
template<typename T>
void list<T>::push_front(T data)
{
if (head != nullptr)
{
Node* p = new Node(nullptr, head, data);
p->next->prev = p;
head = p;
}
else
{
Node* p = new Node(nullptr, nullptr, data);
head = p;
tail = p;
}
++elements;
}
template<typename T>
void list<T>::push_back(T data)
{
if (tail != nullptr)
{
Node* p = new Node(tail, nullptr, data);
p->prev->next = p;
tail = p;
}
else
{
Node* p = new Node(nullptr, nullptr, data);
head = p;
tail = p;
}
++elements;
}
template<typename T>
void list<T>::pop_front()
{
if (head == nullptr)
{
cout << "Empty list! Cannot delete!" << endl;
return;
}
if (elements != 1)
{
head = head->next;
delete head->prev;
head->prev = nullptr;
}
else
{
delete head;
head = nullptr;
tail = nullptr;
}
--elements;
}
template<typename T>
void list<T>::pop_back()
{
if (tail == nullptr)
{
cout << "Empty list! Cannot delete!" << endl;
return;
}
if (elements != 1)
{
tail = tail->prev;
delete tail->next;
tail->next = nullptr;
}
else
{
delete tail;
head = nullptr;
tail = nullptr;
}
--elements;
}
template<typename T>
T list<T>::front()
{
return head->data;
}
template<typename T>
T list<T>::back()
{
return tail->data;
}
template<typename T>
T list<T>::value(int i)
{
return position(i)->data;
}
template<typename T>
T list<T>::rvalue(int i)
{
return rposition(i)->data;
}
template<typename T>
T list<T>::min()
{
if (elements == 1)
return head->data;
Node* p = head->next;
T minimum = head->data;
while (p != nullptr)
{
if (p->data < minimum)
minimum = p->data;
p = p->next;
}
return minimum;
}
template<typename T>
T list<T>::max()
{
if (elements == 1)
return head->data;
Node* p = head->next;
T maximum = head->data;
while (p != nullptr)
{
if (p->data > maximum)
maximum = p->data;
p = p->next;
}
return maximum;
}
template<typename T>
void list<T>::clear()
{
list<T>::~list();
}
template<typename T>
bool list<T>::empty()
{
return elements == 0;
}
template<typename T>
int list<T>::size()
{
return elements;
}
template<typename T>
void list<T>::swap(int i, int j)
{
if (i < 0 || i >= elements || j < 0 || j >= elements)
{
cout << "Error! Non-existent positions!" << endl;
return;
}
Node* p = position(i);
Node* q = position(j);
T data = p->data;
p->data = q->data;
q->data = data;
}
template<typename T>
void list<T>::rswap(int i, int j)
{
if (i < 0 || i >= elements || j < 0 || j >= elements)
{
cout << "Error! Non-existent positions!" << endl;
return;
}
Node* p = rposition(i);
Node* q = rposition(j);
T data = p->data;
p->data = q->data;
q->data = data;
}
template<typename T>
void list<T>::replace(T x, T y)
{
Node* p = head;
while (p != nullptr)
{
if (p->data == x)
p->data = y;
p = p->next;
}
}
template<typename T>
void list<T>::remove(T x)
{
if (head == nullptr)
{
cout << "Empty list! Cannot delete!" << endl;
return;
}
if (elements == 1)
{
if (head->data == x)
pop_front();
return;
}
if (elements == 2)
{
if (head->data == x)
pop_front();
if (tail->data == x)
pop_back();
return;
}
Node* p = head->next;
Node* q;
while (p != tail)
{
if (p->data == x)
{
q = p;
p = p->next;
q->prev->next = q->next;
q->next->prev = q->prev;
delete q;
--elements;
}
else
p = p->next;
}
if (head->data == x)
pop_front();
if (tail != nullptr && tail->data == x)
pop_back();
}
template<typename T>
void list<T>::reverse()
{
int n = elements / 2;
Node* p = head;
Node* q = tail;
T data;
for (int i = 0; i < n; ++i)
{
data = p->data;
p->data = q->data;
q->data = data;
p = p->next;
q = q->prev;
}
}
template<typename T>
void list<T>::resize(int n)
{
if (n <= 0)
{
cout << "Error! Cannot resize!" << endl;
return;
}
if (elements > n)
erase(n, elements - 1);
else
if (elements < n)
for (int i = elements; i < n; ++i)
push_back(0);
}
template<typename T>
void list<T>::rresize(int n)
{
if (n <= 0)
{
cout << "Error! Cannot resize!" << endl;
return;
}
if (elements > n)
rerase(n, elements - 1);
else
if (elements < n)
for (int i = elements; i < n; ++i)
push_front(0);
}
template<typename T>
void list<T>::resize(int n, T x)
{
if (n <= 0)
{
cout << "Error! Cannot resize!" << endl;
return;
}
if (elements > n)
erase(n, elements - 1);
Node* p = head;
for (int i = 0; i < elements; ++i)
{
p->data = x;
p = p->next;
}
if (elements < n)
for (int i = elements; i < n; ++i)
push_back(x);
}
template<typename T>
bool list<T>::contains(T x)
{
Node* p = head;
while (p != nullptr)
{
if (p->data == x)
return true;
p = p->next;
}
return false;
}
template<typename T>
void list<T>::sort()
{
quick_sort(0, elements - 1);
}
template<typename T>
void list<T>::rsort()
{
quick_rsort(0, elements - 1);
}
template<typename T>
bool list<T>::ascending()
{
if (elements == 1)
return true;
T x = head->data;
Node* p = head->next;
while (p != nullptr)
{
if (x > p->data)
return false;
x = p->data;
p = p->next;
}
return true;
}
template<typename T>
bool list<T>::descending()
{
if (elements == 1)
return true;
T x = head->data;
Node* p = head->next;
while (p != nullptr)
{
if (x < p->data)
return false;
x = p->data;
p = p->next;
}
return true;
}
template<typename T>
void list<T>::unique()
{
if (elements == 1)
return;
if (elements == 2)
{
if (head->data == tail->data)
pop_front();
return;
}
if (!ascending() && !descending())
{
cout << "Error! The list is not sorted!" << endl;
return;
}
Node* p = head->next;
Node* q;
while (p->next != nullptr)
{
q = p;
while (q->next != nullptr && q->data == p->data)
q = q->next;
if (p->next != q)
{
destroy(p->next, q->prev);
p->next = q;
q->prev = p;
}
p = q;
}
if (head->data == head->next->data)
pop_front();
if (elements != 1 && tail->data == tail->prev->data)
pop_back();
}
template<typename T>
void list<T>::insert_before(int i, T data)
{
if (i < 0 || i >= elements)
{
cout << "Error! Cannot insert element!" << endl;
return;
}
if (i == 0)
push_front(data);
else
{
Node* p = position(i);
Node* a = new Node(p->prev, p, data);
p->prev->next = a;
p->prev = a;
}
++elements;
}
template<typename T>
void list<T>::insert_after(int i, T data)
{
if (i < 0 || i >= elements)
{
cout << "Error! Cannot insert element!" << endl;
return;
}
if (i == elements - 1)
push_back(data);
else
{
Node* p = position(i);
Node* a = new Node(p, p->next, data);
p->next->prev = a;
p->next = a;
}
++elements;
}
template<typename T>
void list<T>::rinsert_before(int i, T data)
{
if (i < 0 || i >= elements)
{
cout << "Error! Cannot insert element!" << endl;
return;
}
if (i == 0)
push_back(data);
else
{
Node* p = rposition(i);
Node* a = new Node(p, p->next, data);
p->next->prev = a;
p->next = a;
}
++elements;
}
template<typename T>
void list<T>::rinsert_after(int i, T data)
{
if (i < 0 || i >= elements)
{
cout << "Error! Cannot insert element!" << endl;
return;
}
if (i == elements - 1)
push_front(data);
else
{
Node* p = rposition(i);
Node* a = new Node(p->prev, p, data);
p->prev->next = a;
p->prev = a;
}
++elements;
}
template<typename T>
void list<T>::insert_before(int i, int n, T x)
{
if (i < 0 || i >= elements)
{
cout << "Error! Cannot insert element!" << endl;
return;
}
if (n <= 0)
{
cout << "Error! List size must be at least 1" << endl;
return;
}
Node* p;
Node* q;
if (i == 0)
{
push_front(x);
p = head->next;
--n;
}
else
p = position(i);
for (int i = 0; i < n; ++i)
{
q = new Node(p->prev, p, x);
p->prev->next = q;
p->prev = q;
p = q;
}
elements += n;
}
template<typename T>
void list<T>::insert_after(int i, int n, T x)
{
if (i < 0 || i >= elements)
{
cout << "Error! Cannot insert element!" << endl;
return;
}
if (n <= 0)
{
cout << "Error! List size must be at least 1" << endl;
return;
}
Node* p;
Node* q;
if (i == elements - 1)
{
push_back(x);
p = tail->prev;
--n;
}
else
p = position(i);
for (int i = 0; i < n; ++i)
{
q = new Node(p, p->next, x);
p->next->prev = q;
p->next = q;
p = q;
}
elements += n;
}
template<typename T>
void list<T>::rinsert_before(int i, int n, T x)
{
if (i < 0 || i >= elements)
{
cout << "Error! Cannot insert element!" << endl;
return;
}
if (n <= 0)
{
cout << "Error! List size must be at least 1" << endl;
return;
}
Node* p;
Node* q;
if (i == 0)
{
push_back(x);
p = tail->prev;
--n;
}
else
p = rposition(i);
for (int i = 0; i < n; ++i)
{
q = new Node(p, p->next, x);
p->next->prev = q;
p->next = q;
p = q;
}
elements += n;
}
template<typename T>
void list<T>::rinsert_after(int i, int n, T x)
{
if (i < 0 || i >= elements)
{
cout << "Error! Cannot insert element!" << endl;
return;
}
if (n <= 0)
{
cout << "Error! List size must be at least 1" << endl;
return;
}
Node* p;
Node* q;
if (i == elements - 1)
{
push_front(x);
p = head->next;
--n;
}
else
p = rposition(i);
for (int i = 0; i < n; ++i)
{
q = new Node(p->prev, p, x);
p->prev->next = q;
p->prev = q;
p = q;
}
elements += n;
}
template<typename T>
void list<T>::erase(int i)
{
if (head == nullptr)
{
cout << "Empty list! Cannot delete!" << endl;
return;
}
if (i < 0 || i >= elements)
{
cout << "Error! Invalid position" << endl;
return;
}
if (i == 0)
pop_front();
else
if (i == elements - 1)
pop_back();
else
{
Node* p = position(i);
p->prev->next = p->next;
p->next->prev = p->prev;
delete p;
--elements;
}
}
template<typename T>
void list<T>::erase(int begin, int end)
{
if (head == nullptr)
{
cout << "Empty list! Cannot delete!" << endl;
return;
}
if (begin < 0 || begin >= elements || end < 0 || end >= elements)
{
cout << "Error! Non-existent interval!" << endl;
return;
}
if (begin > end)
swap_pos(begin, end);
if (begin == 0 && end == elements - 1)
{
list<T>::~list();
return;
}