LList.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //---------------------------------------------------------------------------
  2. // LList.cpp -- Prototype for linked list class
  3. //
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. //===========================================================================//
  7. //---------------------------------------------------------------------------
  8. // Include Files
  9. #ifndef LLIST_H
  10. #include "llist.h"
  11. #endif
  12. //---------------------------------------------------------------------------
  13. // Class LinkedList
  14. void LinkedList::AddToHead (Link *Node)
  15. {
  16. if (Node == NULL)
  17. {
  18. return;
  19. }
  20. Node -> Next = Head;
  21. Head = Node;
  22. if (Tail == NULL)
  23. {
  24. Tail = Head;
  25. }
  26. }
  27. //---------------------------------------------------------------------------
  28. void LinkedList::AddToTail (Link *Node)
  29. {
  30. if (Node == NULL)
  31. {
  32. return;
  33. }
  34. if (Tail != NULL)
  35. {
  36. Tail -> Next = Node;
  37. }
  38. else
  39. {
  40. Head = Node;
  41. }
  42. Tail = Node;
  43. }
  44. //---------------------------------------------------------------------------
  45. void LinkedList::Destroy (Link *Node, Link *Last)
  46. {
  47. Remove (Node, Last);
  48. delete Node;
  49. }
  50. //---------------------------------------------------------------------------
  51. void LinkedList::InsertAfter (Link* PlaceTo, Link *NodeToPut)
  52. {
  53. if (PlaceTo == NULL || NodeToPut == NULL)
  54. {
  55. return;
  56. }
  57. if (PlaceTo != Tail)
  58. {
  59. NodeToPut -> Next = PlaceTo -> Next;
  60. PlaceTo -> Next = NodeToPut;
  61. return;
  62. }
  63. else
  64. {
  65. Tail = NodeToPut;
  66. NodeToPut -> Next = NULL;
  67. PlaceTo -> Next = NodeToPut;
  68. }
  69. }
  70. //---------------------------------------------------------------------------
  71. inline void LinkedList::MoveAfter (Link *PlaceTo, Link *NodeToPut, Link *Last)
  72. {
  73. Remove (NodeToPut, Last);
  74. InsertAfter (PlaceTo, NodeToPut);
  75. }
  76. //---------------------------------------------------------------------------
  77. void LinkedList::Remove (Link *Node, Link *Last)
  78. {
  79. Link *Search;
  80. if (Last == NULL)
  81. {
  82. Search = Head;
  83. while (Search != NULL && Search != Node)
  84. {
  85. Last = Search;
  86. Search = Search -> Next;
  87. }
  88. if (Search == NULL)
  89. return; // the node wasn't in the list
  90. }
  91. if (Last == NULL)
  92. {
  93. Head = Head -> Next;
  94. if (!Head)
  95. Tail = NULL;
  96. return;
  97. }
  98. if (Tail != Node)
  99. {
  100. Last -> Next = Node -> Next;
  101. if (Head == Node)
  102. {
  103. Head = Node -> Next;
  104. }
  105. }
  106. else // then Tail == Node
  107. {
  108. Tail = Last;
  109. Last -> Next = NULL;
  110. }
  111. }
  112. //---------------------------------------------------------------------------
  113. void LinkedList::Kill (void)
  114. {
  115. Link *Temp;
  116. while (Head != NULL)
  117. {
  118. Temp = Head -> Next;
  119. delete Head;
  120. Head = Temp;
  121. }
  122. Head = Tail = NULL;
  123. }
  124. //---------------------------------------------------------------------------
  125. bool LinkedList::Traverse (Link *&Current)
  126. {
  127. if (Current == NULL)
  128. {
  129. Current = Head;
  130. }
  131. else
  132. {
  133. Current = Current -> Next;
  134. }
  135. return (Current != NULL);
  136. }
  137. //---------------------------------------------------------------------------
  138. unsigned long LinkedList::Count (void)
  139. {
  140. unsigned long Counter = 0;
  141. Link *Current = Head;
  142. while (Current != NULL)
  143. {
  144. Counter ++;
  145. Current = Current -> Next;
  146. }
  147. return Counter;
  148. }
  149. //---------------------------------------------------------------------------