#include template class List { public: struct Node { Node* next; T info; }; class Iterator { private: Node* current; public: explicit Iterator(Node* c) { current = c; } T& operator*() const { return current->info; } T* operator->() const { return ¤t->info; } void operator++() { current = current->next; } bool hasNext() const { return current->next != nullptr; } }; private: Node* first; Node** pLast; public: List() { first = nullptr; pLast = &first; } void add(T val) { *pLast = new Node; (*pLast)->next = nullptr; (*pLast)->info = val; pLast = &(*pLast)->next; } bool isEmpty() const { return first == nullptr; } Iterator iterator() { return Iterator(first); } }; int main() { List list; list.add(10); list.add(12); list.add(42); if(!list.isEmpty()) { List::Iterator it=list.iterator(); while(true) { *it = *it * 10; if(!it.hasNext()) break; ++it; } } printf("Begin list\n"); if(!list.isEmpty()) { List::Iterator it=list.iterator(); while(true) { printf(" %d\n", *it); if(!it.hasNext()) break; ++it; } } printf("End list\n"); }