LinkedList.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. Double-Linked List Class
  8. Caution: The entire library relies on this base class. Modify with care!
  9. --------------------------------------------------------------------------- */
  10. #include "nde.h"
  11. #include "LinkedList.h"
  12. //---------------------------------------------------------------------------
  13. LinkedListEntry *LinkedListEntry::GetNext(void) const
  14. {
  15. return Next;
  16. }
  17. //---------------------------------------------------------------------------
  18. LinkedListEntry *LinkedListEntry::GetPrevious(void) const
  19. {
  20. return Previous;
  21. }
  22. //---------------------------------------------------------------------------
  23. LinkedListEntry::LinkedListEntry()
  24. {
  25. //Int=0;
  26. Next=NULL;
  27. Previous=NULL;
  28. }
  29. //---------------------------------------------------------------------------
  30. LinkedListEntry::~LinkedListEntry()
  31. {
  32. }
  33. //---------------------------------------------------------------------------
  34. LinkedList::LinkedList()
  35. {
  36. Head = NULL;
  37. Foot = NULL;
  38. NElements=0;
  39. }
  40. //---------------------------------------------------------------------------
  41. LinkedList::~LinkedList()
  42. {
  43. while (Head)
  44. RemoveEntry(Head);
  45. }
  46. //---------------------------------------------------------------------------
  47. void LinkedList::AddEntry(LinkedListEntry *Entry, bool Cat)
  48. {
  49. #ifdef WIN32
  50. //EnterCriticalSection(&Critical);
  51. #endif
  52. if (!Cat)
  53. {
  54. if (Head)
  55. Head->Previous = Entry;
  56. Entry->Next = Head;
  57. Entry->Previous = NULL;
  58. Head = Entry;
  59. if (!Foot)
  60. Foot = Entry;
  61. }
  62. else
  63. {
  64. if (Foot)
  65. Foot->Next = Entry;
  66. Entry->Previous = Foot;
  67. Entry->Next = NULL;
  68. Foot = Entry;
  69. if (!Head)
  70. Head = Entry;
  71. }
  72. NElements++;
  73. #ifdef WIN32
  74. //LeaveCriticalSection(&Critical);
  75. #endif
  76. }
  77. //---------------------------------------------------------------------------
  78. void LinkedList::RemoveEntry(LinkedListEntry *Entry)
  79. {
  80. if (!Entry) return;
  81. #ifdef WIN32
  82. //EnterCriticalSection(&Critical);
  83. #endif
  84. if (Entry->Next)
  85. Entry->Next->Previous = Entry->Previous;
  86. else
  87. Foot = Entry->Previous;
  88. if (Entry->Previous)
  89. Entry->Previous->Next = Entry->Next;
  90. else
  91. Head = Entry->Next;
  92. if (NElements > 0)
  93. {
  94. delete Entry;
  95. NElements--;
  96. }
  97. #ifdef WIN32
  98. //LeaveCriticalSection(&Critical);
  99. #endif
  100. }
  101. //---------------------------------------------------------------------------
  102. void LinkedList::WalkList(WalkListProc WalkProc, int ID, void *Data1, void *Data2)
  103. {
  104. if (!WalkProc) return;
  105. LinkedListEntry *Entry = Head;
  106. while (Entry)
  107. {
  108. if (!WalkProc(Entry, ID, Data1, Data2)) break;
  109. Entry = Entry->Next;
  110. }
  111. }