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.

106 lines
1.7 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. slist.inl
  6. Abstract:
  7. Single linked list template class.
  8. Author:
  9. Weihai Chen (WeihaiC) 06/29/98
  10. Revision History:
  11. --*/
  12. template <class T>
  13. CSingleList<T>::CSingleList<T>(void)
  14. :m_Dummy ()
  15. {
  16. }
  17. template <class T>
  18. CSingleList<T>::~CSingleList<T>(void)
  19. {
  20. Lock ();
  21. CSingleItem<T> *pItem = m_Dummy.GetNext();
  22. CSingleItem<T> *pNext;
  23. while (pItem) {
  24. pNext = pItem->GetNext();
  25. delete pItem;
  26. pItem = pNext;
  27. }
  28. Unlock ();
  29. }
  30. template <class T>
  31. BOOL CSingleList<T>::Insert (T item)
  32. {
  33. BOOL bRet = FALSE;
  34. Lock();
  35. CSingleItem<T> *pNewItem = new CSingleItem<T> (item, m_Dummy.GetNext());
  36. if (pNewItem) {
  37. m_Dummy.SetNext (pNewItem);
  38. bRet = TRUE;
  39. }
  40. Unlock();
  41. return bRet;
  42. }
  43. template <class T>
  44. BOOL CSingleList<T>::Delete (T item)
  45. {
  46. CSingleItem<T> *pHead = &m_Dummy;
  47. CSingleItem<T> *pItem;
  48. BOOL bRet = FALSE;
  49. Lock();
  50. while (pItem = pHead->GetNext()) {
  51. if (pItem->IsSame (item)) {
  52. // Delete it
  53. pHead->SetNext (pItem->GetNext ());
  54. delete (pItem);
  55. bRet = TRUE;
  56. break;
  57. }
  58. pHead = pHead->GetNext();
  59. }
  60. Unlock();
  61. return FALSE;
  62. }
  63. template <class T>
  64. T CSingleList<T>::Find (T item)
  65. {
  66. Lock();
  67. CSingleItem<T> *pItem = m_Dummy.GetNext();
  68. while (pItem) {
  69. if (pItem->IsSame (item)) {
  70. Unlock ();
  71. return pItem->GetData();
  72. }
  73. pItem = pItem->GetNext();
  74. }
  75. Unlock ();
  76. return NULL;
  77. }