@@ -43,14 +43,15 @@ class forward_lists{
43
43
*/
44
44
template <typename It>
45
45
requires my_input_iterator<It>
46
- forward_lists (It begin,It end): head( nullptr ),size( 0 ) {
46
+ forward_lists (It begin,It end){
47
47
head = new Node (T{});
48
48
size = 0 ;
49
49
head->next = nullptr ;
50
50
tail = head;
51
- Node** curr = &tail ->next ; // curr menunjuk head->next
51
+ Node** curr = &head ->next ; // curr menunjuk head->next
52
52
while (begin != end){
53
- *curr = new Node (*begin); // isi node
53
+ *curr = new Node (*begin); // isi node dengan deferencing begin
54
+ tail = *curr;
54
55
curr = &((*curr)->next ); // curr = curr->next
55
56
size++; // increment size
56
57
++begin; // increment iterator
@@ -65,10 +66,11 @@ class forward_lists{
65
66
head->next = nullptr ;
66
67
tail = head;
67
68
size = 0 ;
68
- Node** curr = &tail ->next ; // store alamat memory head ke curr
69
+ Node** curr = &head ->next ; // store alamat memory head ke curr
69
70
for (const T& it: arr){
70
71
*curr = new Node (it); // deference pointer
71
72
// head = new Node(it) ->meaning
73
+ tail = *curr;
72
74
curr = &((*curr)->next ); // store alamat curr->next
73
75
// curr = head->next
74
76
// curr selalu menunjuk ke posisi kosong
@@ -119,21 +121,29 @@ class forward_lists{
119
121
* karena head = others.head maka head == nullptr,akan mengakibatkan
120
122
* undefined behavior
121
123
*/
122
- forward_lists (forward_lists&& others) noexcept : head(nullptr ){
123
- head = others.head ;
124
+ forward_lists (forward_lists&& others) noexcept : head(new Node(T{})),tail(head),size(0 ){
125
+ head->next = others.head ->next ;
126
+ tail = others.tail == head ? head: others.tail ; // ternery operator,if you dont understand please read documentation
124
127
size = others.size ;
125
- others.head = nullptr ; // kosongkan others lama agar tidak memory leak
128
+
129
+ // kosongkan object lama
130
+ others.head ->next = nullptr ;
131
+ others.tail = others.head ;
126
132
others.size = 0 ;
127
133
}
128
134
/* *
129
135
* @brief move assignment constructor
130
136
*/
131
137
forward_lists& operator =(forward_lists&& others)noexcept {
132
- if (this != others){
138
+ if (this != & others){
133
139
clear ();
134
- head = others.head ;
140
+ head->next = others.head ->next ;
141
+ tail = others.tail == head ? head: others.tail ; // ternery operator,if you dont understand please read documentation
135
142
size = others.size ;
136
- others.head = nullptr ;
143
+
144
+ // kosongkan object lama
145
+ others.head ->next = nullptr ;
146
+ others.tail = others.head ;
137
147
others.size = 0 ;
138
148
}
139
149
return *this ;
@@ -250,6 +260,9 @@ class forward_lists{
250
260
std::size_t max_size ()const noexcept {
251
261
return std::numeric_limits<T>::max () / sizeof (Node);
252
262
}
263
+ T back ()const noexcept {
264
+ return this ->tail ->data ;
265
+ }
253
266
public:
254
267
/* *
255
268
* @brief method untuk insertion val pada pos front
@@ -288,15 +301,14 @@ class forward_lists{
288
301
void pop_front (){
289
302
if (tail == head){
290
303
return ;
291
- }else if (head->next == tail->next ){
292
- head = tail = nullptr ;
293
- size--;
294
- }else {
295
- Node* temp = head->next ;
296
- Node* next = head->next ;
297
- head->next = next->next ;
298
- delete temp;
299
304
}
305
+ Node* temp = head->next ;
306
+ head->next = temp->next ;
307
+ if (temp == tail){
308
+ tail = head;
309
+ }
310
+ delete temp;
311
+ size--;
300
312
}
301
313
/* *
302
314
* @brief Menyisipkan elemen setelah posisi iterator tertentu.
@@ -551,7 +563,7 @@ class forward_lists{
551
563
head = new Node (T{});
552
564
head->next = nullptr ;
553
565
Node** curr = &head->next ;
554
- size++;
566
+ // size++;
555
567
for (const T& value: arr){
556
568
*curr = new Node (value); // curr = new Node
557
569
curr = &((*curr)->next ); // curr = curr->next
@@ -602,18 +614,25 @@ class forward_lists{
602
614
}
603
615
void splice_after (const Iterator pos,forward_lists& others){
604
616
Node* src = pos.get_raw ();// pos object saat ini
605
- Node* begin = others.begin (); // begin object lain
606
- Node* end = others.end ();
607
- // hubungkan node terakhir ke object saat ini
608
- Node* moved = src->next ; // object saat ini
609
- end->next = moved; // end->next = src->next
610
- // hubungkan ke list tujuan
611
- Node* curr = src; // curr menunjuk src
612
- curr->next = begin;// curr next adalah begin dari object lain
613
- // moved->next = begin; //object saat ini
614
- size = others.size ;
617
+ Node* begin = others.head ->next ; // begin object lain
618
+ Node* end = others.tail ; // end object lain
619
+
620
+ // lingking node pertama others ke object saat ini
621
+ Node* moved = src->next ;
622
+ moved->next = begin->next ; // tunjuk node setelah begin
623
+
624
+ begin->next = moved; // linking begin ke node setelah iterator
625
+ // linking node terakhir object lain ke src->next
626
+ end->next = src->next ;
627
+
628
+ // linking node pertama object saat ini ke head object lain
629
+ src->next = begin;
630
+ size += others.size ;
615
631
others.size = 0 ;
616
632
}
633
+ // void splice_after(const Iterator pos,forward_lists& others,const Iterator first,const Iterator last){
634
+ // Node* curr = pos.get_raw();
635
+ // }
617
636
public:
618
637
/* *
619
638
* @brief method untuk print semua node list
@@ -632,13 +651,16 @@ class forward_lists{
632
651
* berguna pada destructor
633
652
* @details time complexity O(n),Space Complexity O(n)
634
653
*/
635
- void clear (){
636
- while (head->next != nullptr ){
637
- Node* temp = head->next ;
638
- head->next = head->next ->next ;
639
- delete temp;
640
- }
641
- size = 0 ;
642
- }
654
+ void clear (){
655
+ Node* curr = head->next ; // mulai dari setelah dummy
656
+ while (curr != nullptr ){
657
+ Node* temp = curr;
658
+ curr = curr->next ;
659
+ delete temp;
660
+ }
661
+ head->next = nullptr ; // dummy menunjuk ke kosong
662
+ tail = head; // tail kembali ke dummy
663
+ size = 0 ;
664
+ }
643
665
};
644
666
#endif
0 commit comments