Skip to content

Commit 9161231

Browse files
authored
Merge pull request #9 from yusuf601/feat/before_begin
feat: menyelesaikan method insert_after
2 parents f040388 + 3bae128 commit 9161231

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

header/forward_list.hpp

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <iostream>
22
#include <initializer_list>
3+
#include <type_traits>
4+
#include <iterator>
35
#ifndef __forwardList
46
#define __forwardList
57
template <typename T>
@@ -17,8 +19,9 @@ class forward_lists{
1719
Node* head;
1820
public:
1921
forward_lists(){
20-
this->head = nullptr;
21-
this->size = 0;
22+
this->head = new Node();
23+
this->head->next = nullptr;
24+
this->size = 1;
2225
}
2326
/**
2427
* @brief initializer list constructor
@@ -77,9 +80,10 @@ class forward_lists{
7780
private:
7881
Node* node;
7982
public:
80-
Iterator(Node* n){
81-
this->node = n;
82-
}
83+
Iterator(Node* n){
84+
this->node = n;
85+
}
86+
friend std::ostream& operator<<(std::ostream& os,const Iterator& it);
8387
T& operator*(){
8488
return node->data;
8589
}
@@ -97,18 +101,24 @@ class forward_lists{
97101
}
98102
};
99103
public: //inialisasi Iterator
100-
Iterator begin(){
104+
Iterator before_begin(){
101105
return Iterator(head);
102106
}
107+
Iterator begin(){
108+
return Iterator(head->next);
109+
}
103110
Iterator end(){
104111
return Iterator(nullptr);
105112
}
106113
Iterator cbegin()const{
107-
return Iterator(head);
114+
return Iterator(head->next);
108115
}
109116
Iterator cend()const{
110117
return Iterator(nullptr);
111118
}
119+
Iterator Cbefore_begin()const{
120+
return Iterator(head);
121+
}
112122
public: //getter
113123
T front(){
114124
return head->data;
@@ -167,11 +177,39 @@ class forward_lists{
167177
curr->next = new_node;
168178
}
169179
}
170-
// void insert_after(Iterator begin,T itr1,T itr2){
171-
// for(auto it = itr1; it != itr2;++it){
172-
// auto value = *it;//deferencing
173-
// }
174-
// }
180+
template<typename pos>
181+
void insert_after(Iterator iter_position,pos itr1,pos itr2){
182+
using category = typename std::iterator_traits<pos>::iterator_category;
183+
static_assert(
184+
std::is_base_of_v<std::input_iterator_tag,category>,
185+
"parameter harus iterator"
186+
);
187+
Node* curr = iter_position;
188+
Node* new_node = new Node(*itr1);
189+
Node* tail = new_node;
190+
++itr1;
191+
while(itr1 != itr2){
192+
Node* n_node = new_node(*itr1);
193+
++itr1;
194+
tail->next = n_node;
195+
tail = n_node;
196+
}
197+
tail->next = curr->next;
198+
curr->next = new_node;
199+
}
200+
void insert_after(Iterator iter_position,Iterator listBegin,Iterator listEnd){
201+
Node* curr = iter_position;
202+
Node* new_node = new Node(*listBegin);
203+
Node* tail = new_node;
204+
++listBegin;
205+
while(listBegin != listEnd){
206+
Node* n_node = new_node(*listBegin);
207+
tail->next = n_node;
208+
tail = n_node;
209+
}
210+
tail->next = curr->next;
211+
curr->next = new_node;
212+
}
175213
public:
176214
void print_all(Iterator begin,Iterator end){
177215
while(begin != end){
@@ -188,5 +226,9 @@ class forward_lists{
188226
}
189227
}
190228
};
191-
229+
template<typename T>
230+
std::ostream& operator<<(std::ostream& os,const typename forward_lists<T>::Iterator& it){
231+
os << *it; //deferencing it
232+
return os;
233+
}
192234
#endif

implementation/implementation.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ int main(){
99
fl.push_front(1);
1010
fl.push_front(2);
1111
fl.push_front(3);
12+
std::cout << std::endl;
13+
//=========== Menggunakan deferencing==============
1214
for(auto it = fl.begin(); it != fl.end(); ++it) {
13-
std::cout << *it << " " << std::endl;
15+
std::cout << *it << " ";
16+
}
17+
//================ Tanpa deferencing ===============
18+
for(auto it = fl.begin();it != fl.end();++it){
19+
std::cout << it << " ";
1420
}
1521
std::cin.get();
1622
return 0;

0 commit comments

Comments
 (0)