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

#include <iostream>
using namespace std;
template <typename T>
class List
{
struct Node
{
T data;
Node* prev = nullptr;
Node*next = nullptr;
Node() = default;
Node(T d) : data(d), prev(nullptr), next(nullptr) {}
};
Node* head = nullptr;
Node *tail = nullptr;
std::size_t l_size = 0;
//SFINAE
template <typename Iter>
using required_input_iterator = std::enable_if<std::is_base_of_v<std::input_iterator_tag,
typename std::iterator_traits<Iter>::iterator_category >>;
public:
using reference = T&;
using const_reference = const T&;
using size_type = std::size_t;
class const_iterator
{
Node* node = nullptr;
friend class List;
const_iterator (Node* p) : node(p) {}
public:
using value_type = std::remove_reference_t<T>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
using iterator_category = std::bidirectional_iterator_tag;
public:
const_iterator () = default;
const_iterator(const const_iterator& other) : node(other.node) {}
reference operator*()
{
return node>data;
}
T* operator>()
{
return node>data;
}
// prefix operators
const_iterator& operator++()
{
node = node>next;
return *this;
}
const_iterator& operator()
{
node = node>prev;
return *this;
}
// postfix operators
const_iterator operator++(int)
{
Node* temp = this>node;
node = node>next;
return temp;
}
const_iterator operator(int)
{
Node* temp = this>node;
node = node>prev;
return temp;
}
friend bool operator== (const const_iterator& lhs, const const_iterator& rhs) noexcept
{
return lhs.node == rhs.node;
}
friend bool operator!= (const const_iterator& lhs, const const_iterator& rhs) noexcept
{
return lhs.node != rhs.node;
}
};
class iterator : public std::input_iterator_tag
{
Node * node = nullptr;
friend class List;
iterator(Node* p) : node(p) {}
public:
using value_type = std::remove_reference_t<T>;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
using iterator_category = std::bidirectional_iterator_tag;
public:
iterator () = default;
reference operator*()
{
return node>data;
}
reference operator>()
{
return node>data;
}
// prefix operators
iterator& operator++()
{
node = node>next;
return *this;
}
iterator& operator()
{
node = node>prev;
return *this;
}
// postfix operators
iterator operator++(int)
{
Node* temp = this>node;
node = node>next;
return temp;
}
iterator operator(int)
{
Node* temp = this>node;
node = node>prev;
return temp;
}
friend bool operator== (const iterator& lhs, const iterator& rhs) noexcept
{
return lhs.node == rhs.node;
}
friend bool operator!= (const iterator& lhs, const iterator& rhs) noexcept
{
return lhs.node != rhs.node;
}
};
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
List() = default;
explicit List(size_type n);
List(size_type n, const_reference v);
List(const std::initializer_list<T>& i_list);
template<typename InputIterator, typename = required_input_iterator<InputIterator>>
List(InputIterator first, InputIterator last);
List(const List& src);
List& operator=(List src);
List(List&& src);
//~List();
// iterators
iterator begin() noexcept
{
return iterator(head);
}
const_iterator begin() const noexcept
{
return iterator(head);
}
iterator end() noexcept
{
return iterator(tail);
}
const_iterator end() const noexcept
{
return iterator(tail);
}
template<typename... Args> iterator emplace(const iterator& pos, Args&&... args );
};
template <typename T>
List<T>::List(const std::initializer_list<T>& i_list) : l_size(i_list.size())
{
if(l_size == 0)
return;
Node* temp = new Node;
head = temp;
temp>prev = nullptr;
for(auto it = i_list.begin(); it != i_list.end(); ++it)
{
temp>data = *it;
temp>next = new Node;
temp>next>prev = temp;
temp = temp>next;
}
tail = temp; // is this correct?
}
template<typename T>
template<typename... Args>
typename List<T>::iterator List<T>::emplace(const iterator& pos, Args&&... args )
{
Node* curr = pos.node;
Node* new_node = new Node(std::move(args)...);
// this is where I am having a really hard time
if(curr == tail)
{
curr = new_node;
curr>next= nullptr;
tail = curr>next;
//curr>next>prev = tail;
}
else
{
curr>prev = new_node;
new_node>next = curr;
head = new_node;
}
}
int main()
{
List<int> lis({1, 2, 3, 4, 5, 6});
for(auto e: lis)
{
cout << e << ' ';
}
cout << '\n';
lis.emplace(lis.begin(), 78); // this line work sas expected
lis.emplace(lis.end(), 78); // this line does't
for(auto e: lis)
{
cout << e << ' ';
}
}
 