template class CLinkedList; template struct CLinkedNode { public: CLinkedNode *pNext; Type that; }; template class CLinkedWalk { public: CLinkedWalk(CLinkedList *plist) : _plist(plist), _pCurr(NULL), _pLast(NULL) {} CLinkedWalk() : _plist(NULL), _pCurr(NULL), _pLast(NULL) {} void Init(CLinkedList *plist) {_plist = plist; _pCurr = _pLast = NULL;} inline BOOL IsFirst(void) { return _pLast == NULL;} inline Type *That(void) {return _pCurr ? &_pCurr->that : NULL;} inline CLinkedNode *Node(void) {return _pCurr;} inline BOOL Step(void) { _pLast = _pCurr; if (_pCurr) _pCurr = _pCurr->pNext; else if (_plist) _pCurr = _plist->_pFirst; return (_pCurr != NULL); } inline CLinkedNode *Remove(void) { CLinkedNode *pRet = _pCurr; if (pRet) { _pCurr = _pCurr->pNext; // update the list if (_pLast) _pLast->pNext = _pCurr; else _plist->_pFirst = _pCurr; } return pRet; } inline void Delete(void) { CLinkedNode *p = Remove(); if (p) delete p; } protected: CLinkedList *_plist; CLinkedNode *_pCurr; CLinkedNode *_pLast; }; template class CLinkedList { public: //methods CLinkedList() :_pFirst(NULL) {} ~CLinkedList() { CLinkedWalk lw(this); while (lw.Step()) { lw.Delete(); } } inline BOOL IsEmpty(void) {return _pFirst == NULL;} inline BOOL Insert(CLinkedNode *p) { p->pNext = _pFirst; _pFirst = p; return TRUE; } inline void Remove(CLinkedNode *pRemove) { CLinkedWalk lw(this); while (lw.Step()) { if (lw.Node() == pRemove) { lw.Remove(); break; } } } protected: CLinkedNode *_pFirst; friend class CLinkedWalk; };