#pragma once template class Iterator { protected: T* current; public: explicit Iterator(T* ptr) : current(ptr) {} T& operator*() const { return *current; } T* operator->() const { return current; } bool operator!=(const Iterator& other) const { return current != other.current; } virtual Iterator& operator++() = 0; }; template class Traversal { T* start; public: explicit Traversal(T* start) : start(start) {} Policy begin() const { return Policy(start); } Policy end() const { return Policy(nullptr); } }; template struct IteratorForward : public Iterator { explicit IteratorForward(T* ptr) : Iterator(first(ptr)) {} static T* first(T* node) { return node; } static T* next(T* node) { return node ? node->get_next() : nullptr; } Iterator& operator++() override { this->current = next(this->current); return *this; } }; template struct IteratorBackward : public Iterator { explicit IteratorBackward(T* ptr) : Iterator(first(ptr)) {} static T* first(T* node) { if (!node) return nullptr; while (node->get_next()) node = node->get_next(); return node; } static T* next(T* node) { return node ? node->get_prev() : nullptr; } Iterator& operator++() override { this->current = next(this->current); return *this; } };