Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.2 KiB

  1. template <class Type> class CLinkedList;
  2. template <class Type> struct CLinkedNode
  3. {
  4. public:
  5. CLinkedNode <Type> *pNext;
  6. Type that;
  7. };
  8. template <class Type> class CLinkedWalk
  9. {
  10. public:
  11. CLinkedWalk(CLinkedList<Type> *plist) : _plist(plist), _pCurr(NULL), _pLast(NULL) {}
  12. CLinkedWalk() : _plist(NULL), _pCurr(NULL), _pLast(NULL) {}
  13. void Init(CLinkedList<Type> *plist) {_plist = plist; _pCurr = _pLast = NULL;}
  14. inline BOOL IsFirst(void) { return _pLast == NULL;}
  15. inline Type *That(void) {return _pCurr ? &_pCurr->that : NULL;}
  16. inline CLinkedNode<Type> *Node(void) {return _pCurr;}
  17. inline BOOL Step(void)
  18. {
  19. _pLast = _pCurr;
  20. if (_pCurr)
  21. _pCurr = _pCurr->pNext;
  22. else if (_plist)
  23. _pCurr = _plist->_pFirst;
  24. return (_pCurr != NULL);
  25. }
  26. inline CLinkedNode<Type> *Remove(void)
  27. {
  28. CLinkedNode<Type> *pRet = _pCurr;
  29. if (pRet)
  30. {
  31. _pCurr = _pCurr->pNext;
  32. // update the list
  33. if (_pLast)
  34. _pLast->pNext = _pCurr;
  35. else
  36. _plist->_pFirst = _pCurr;
  37. }
  38. return pRet;
  39. }
  40. inline void Delete(void)
  41. {
  42. CLinkedNode<Type> *p = Remove();
  43. if (p)
  44. delete p;
  45. }
  46. protected:
  47. CLinkedList<Type> *_plist;
  48. CLinkedNode<Type> *_pCurr;
  49. CLinkedNode<Type> *_pLast;
  50. };
  51. template <class Type> class CLinkedList
  52. {
  53. public: //methods
  54. CLinkedList() :_pFirst(NULL) {}
  55. ~CLinkedList()
  56. {
  57. CLinkedWalk<Type> lw(this);
  58. while (lw.Step())
  59. {
  60. lw.Delete();
  61. }
  62. }
  63. inline BOOL IsEmpty(void) {return _pFirst == NULL;}
  64. inline BOOL Insert(CLinkedNode<Type> *p)
  65. {
  66. p->pNext = _pFirst;
  67. _pFirst = p;
  68. return TRUE;
  69. }
  70. inline void Remove(CLinkedNode<Type> *pRemove)
  71. {
  72. CLinkedWalk<Type> lw(this);
  73. while (lw.Step())
  74. {
  75. if (lw.Node() == pRemove)
  76. {
  77. lw.Remove();
  78. break;
  79. }
  80. }
  81. }
  82. protected:
  83. CLinkedNode <Type> *_pFirst;
  84. friend class CLinkedWalk<Type>;
  85. };