1
1
#include < iostream>
2
2
#include < initializer_list>
3
+ #include < type_traits>
4
+ #include < iterator>
3
5
#ifndef __forwardList
4
6
#define __forwardList
5
7
template <typename T>
@@ -17,8 +19,9 @@ class forward_lists{
17
19
Node* head;
18
20
public:
19
21
forward_lists (){
20
- this ->head = nullptr ;
21
- this ->size = 0 ;
22
+ this ->head = new Node ();
23
+ this ->head ->next = nullptr ;
24
+ this ->size = 1 ;
22
25
}
23
26
/* *
24
27
* @brief initializer list constructor
@@ -77,9 +80,10 @@ class forward_lists{
77
80
private:
78
81
Node* node;
79
82
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);
83
87
T& operator *(){
84
88
return node->data ;
85
89
}
@@ -97,18 +101,24 @@ class forward_lists{
97
101
}
98
102
};
99
103
public: // inialisasi Iterator
100
- Iterator begin (){
104
+ Iterator before_begin (){
101
105
return Iterator (head);
102
106
}
107
+ Iterator begin (){
108
+ return Iterator (head->next );
109
+ }
103
110
Iterator end (){
104
111
return Iterator (nullptr );
105
112
}
106
113
Iterator cbegin ()const {
107
- return Iterator (head);
114
+ return Iterator (head-> next );
108
115
}
109
116
Iterator cend ()const {
110
117
return Iterator (nullptr );
111
118
}
119
+ Iterator Cbefore_begin ()const {
120
+ return Iterator (head);
121
+ }
112
122
public: // getter
113
123
T front (){
114
124
return head->data ;
@@ -167,11 +177,39 @@ class forward_lists{
167
177
curr->next = new_node;
168
178
}
169
179
}
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
+ }
175
213
public:
176
214
void print_all (Iterator begin,Iterator end){
177
215
while (begin != end){
@@ -188,5 +226,9 @@ class forward_lists{
188
226
}
189
227
}
190
228
};
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
+ }
192
234
#endif
0 commit comments