//////////////////////////////////////////////////////////////////////// // // CIterator.h: ist (Templated Class) // // Author: Christian Benavides // // Description: // -Contains the definition and implementation of the Iterator. // It is used to go through the Linked List //////////////////////////////////////////////////////////////////////// #pragma once #include "CList.h" template class CIterator { /// Friendly relationship with the list friend class CList < T >; typename CList::TNode* m_pCurrent; typename CList::TNode* m_pPrev; CList* m_pList; public: CIterator(CList& _pList); ~CIterator(); void Begin(); // Set the iterator to the head bool End(); // Returns a flag indicating if we are at the end of the list T& GetCurrentValue() const { return m_pCurrent->m_data; }; void SetValue(const T& _value) { m_pCurrent->m_data = _value; }; CIterator& operator++(); }; //////////////////////////////////////////////////////////////////////// // ITERATOR IMPLEMENTATION //////////////////////////////////////////////////////////////////////// template CIterator::CIterator(CList& _pList) { m_pList = &_pList; Begin(); } template CIterator::~CIterator() { } /// Sets the iterator to the head of the list template void CIterator::Begin() { if (m_pList && m_pList->m_pHead) { m_pCurrent = m_pList->m_pHead; m_pPrev = nullptr; } } /// Flag that returns if the iterator is at the end template bool CIterator::End() { if (m_pCurrent) return false; return true; } /// Advance to the next node in the list. template CIterator& CIterator::operator++() { if (End()) return *this; /// Update both the previous and current pointer m_pPrev = m_pCurrent; m_pCurrent = m_pCurrent->m_pNext; return *this; }