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.

113 lines
2.1 KiB

  1. // Copyright (c) 1998-1999 Microsoft Corporation
  2. //
  3. // alist.cpp
  4. //
  5. #include <windows.h>
  6. #include "alist.h"
  7. LONG AListItem::GetCount(void) const
  8. {
  9. LONG l;
  10. const AListItem *li;
  11. for(l=0,li=this; li!=NULL ; li=li->m_pNext,++l);
  12. return l;
  13. }
  14. AListItem* AListItem::Cat(AListItem *pItem)
  15. {
  16. AListItem *li;
  17. if(this==NULL)
  18. return pItem;
  19. for(li=this ; li->m_pNext!=NULL ; li=li->m_pNext);
  20. li->m_pNext=pItem;
  21. return this;
  22. }
  23. //////////////////////////////////////////////////////////////////////
  24. // AListItem::Remove
  25. AListItem* AListItem::Remove(AListItem *pItem)
  26. {
  27. AListItem *li,*prev;
  28. //treat remove(NULL) same as item not found in list
  29. if (pItem==NULL)
  30. return this;
  31. if(pItem==this)
  32. return m_pNext;
  33. prev=NULL;
  34. for(li=this; li!=NULL && li!=pItem ; li=li->m_pNext)
  35. prev=li;
  36. if(li==NULL) // item not found in list
  37. return this;
  38. // here it is guaranteed that prev is non-NULL since we checked for
  39. // that condition at the very beginning
  40. prev->SetNext(li->m_pNext);
  41. li->SetNext(NULL);
  42. // SetNext on pItem to NULL
  43. pItem->SetNext(NULL);
  44. return this;
  45. }
  46. AListItem* AListItem::GetPrev(AListItem *pItem) const
  47. {
  48. const AListItem *li,*prev;
  49. prev=NULL;
  50. for(li=this ; li!=NULL && li!=pItem ; li=li->m_pNext)
  51. prev=li;
  52. return (AListItem*)prev;
  53. }
  54. AListItem * AListItem::GetItem(LONG index)
  55. {
  56. AListItem *scan;
  57. for (scan = this; scan!=NULL && index; scan = scan->m_pNext)
  58. {
  59. index--;
  60. }
  61. return (scan);
  62. }
  63. void AList::InsertBefore(AListItem *pItem,AListItem *pInsert)
  64. {
  65. AListItem *prev = GetPrev(pItem);
  66. pInsert->SetNext(pItem);
  67. if (prev) prev->SetNext(pInsert);
  68. else m_pHead = pInsert;
  69. }
  70. void AList::AddTail(AListItem *pItem)
  71. {
  72. if (m_pHead == NULL)
  73. {
  74. AddHead(pItem);
  75. }
  76. else
  77. {
  78. m_pHead = m_pHead->AddTail(pItem);
  79. }
  80. }
  81. void AList::Reverse()
  82. {
  83. AList Temp;
  84. AListItem *pItem;
  85. while (pItem = RemoveHead())
  86. {
  87. Temp.AddHead(pItem);
  88. }
  89. Cat(Temp.GetHead());
  90. }