#include struct Node { int value; Node* next; }; class Iterator { public: Iterator(Node* current) : current {current} {} int& operator*() { return current->value; } Iterator& operator++() { current = current->next; return *this; } bool operator==(Iterator const& rhs) const { return current == rhs.current; } private: Node* current; }; class List { public: List() : top {} {} List(List const&) = delete; List& operator=(List const&) = delete; List(List &&) = delete; List& operator=(List &&) = delete; ~List() { while (top != nullptr) { Node* tmp {top->next}; delete top; top = tmp; } } void add(int i) { top = new Node{i, top}; } Iterator begin() { return Iterator{top}; } Iterator end() { return Iterator{nullptr}; } private: Node* top; }; int main() { List list {}; list.add(3); list.add(2); list.add(7); list.add(4); list.add(1); *(++list.begin()) = 12; for (auto it {list.begin()}; !(it == list.end()); ++it) { std::cout << *it << " "; } std::cout << std::endl; // Expected output: 1 12 7 2 3 }