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.

184 lines
3.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: voidlist.cpp
  8. //
  9. // Contents: list functions
  10. //
  11. // History: 01-Jan-2000 reidk created
  12. //
  13. //--------------------------------------------------------------------------
  14. #include "global.hxx"
  15. #include "voidlist.h"
  16. void
  17. LIST_Initialize(LIST *pList)
  18. {
  19. pList->pHead = NULL;
  20. pList->pTail = NULL;
  21. pList->dwNumNodes = 0;
  22. }
  23. PLIST_NODE
  24. LIST_AddHead(LIST *pList, void *pElement)
  25. {
  26. LIST_NODE *pListNode = NULL;
  27. if (NULL == (pListNode = (PLIST_NODE) malloc(sizeof(LIST_NODE))))
  28. {
  29. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  30. return NULL;
  31. }
  32. // empty list
  33. if (pList->pHead == NULL)
  34. {
  35. pList->pHead = pList->pTail = pListNode;
  36. pListNode->pNext = NULL;
  37. }
  38. else
  39. {
  40. pListNode->pNext = pList->pHead;
  41. pList->pHead = pListNode;
  42. }
  43. pList->dwNumNodes++;
  44. pListNode->pElement = pElement;
  45. return(pListNode);
  46. }
  47. PLIST_NODE
  48. LIST_AddTail(LIST *pList, void *pElement)
  49. {
  50. LIST_NODE *pListNode = NULL;
  51. if (NULL == (pListNode = (PLIST_NODE) malloc(sizeof(LIST_NODE))))
  52. {
  53. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  54. return NULL;
  55. }
  56. // empty list
  57. if (pList->pTail == NULL)
  58. {
  59. pList->pHead = pList->pTail = pListNode;
  60. }
  61. else
  62. {
  63. pList->pTail->pNext = pListNode;
  64. pList->pTail = pListNode;
  65. }
  66. pList->dwNumNodes++;
  67. pListNode->pNext = NULL;
  68. pListNode->pElement = pElement;
  69. return(pListNode);
  70. }
  71. BOOL
  72. LIST_RemoveElement(LIST *pList, void *pElement)
  73. {
  74. LIST_NODE *pListNodeToDelete = pList->pHead;
  75. LIST_NODE *pPrevListNode = pList->pHead;
  76. // empty list
  77. if (pListNodeToDelete == NULL)
  78. {
  79. return FALSE;
  80. }
  81. // remove head
  82. if (pListNodeToDelete->pElement == pElement)
  83. {
  84. // one element
  85. if (pList->pHead == pList->pTail)
  86. {
  87. pList->pHead = pList->pTail = NULL;
  88. }
  89. else
  90. {
  91. pList->pHead = (PLIST_NODE) pListNodeToDelete->pNext;
  92. }
  93. }
  94. else
  95. {
  96. pListNodeToDelete = (PLIST_NODE) pListNodeToDelete->pNext;
  97. while ( (pListNodeToDelete != NULL) &&
  98. (pListNodeToDelete->pElement != pElement))
  99. {
  100. pPrevListNode = pListNodeToDelete;
  101. pListNodeToDelete = (PLIST_NODE) pListNodeToDelete->pNext;
  102. }
  103. if (pListNodeToDelete == NULL)
  104. {
  105. return FALSE;
  106. }
  107. pPrevListNode->pNext = pListNodeToDelete->pNext;
  108. // removing tail
  109. if (pList->pTail == pListNodeToDelete)
  110. {
  111. pList->pTail = pPrevListNode;
  112. }
  113. }
  114. pList->dwNumNodes--;
  115. free(pListNodeToDelete);
  116. return(TRUE);
  117. }
  118. BOOL
  119. LIST_RemoveAll(LIST *pList)
  120. {
  121. LIST_NODE *pListNodeToDelete = pList->pHead;
  122. LIST_NODE *pNextListNode = NULL;
  123. while (pListNodeToDelete != NULL)
  124. {
  125. pNextListNode = (PLIST_NODE) pListNodeToDelete->pNext;
  126. free(pListNodeToDelete);
  127. pListNodeToDelete = pNextListNode;
  128. }
  129. pList->pHead = pList->pTail = NULL;
  130. pList->dwNumNodes = 0;
  131. return(TRUE);
  132. }
  133. PLIST_NODE
  134. LIST_GetFirst(LIST *pList)
  135. {
  136. return(pList->pHead);
  137. }
  138. PLIST_NODE
  139. LIST_GetNext(PLIST_NODE pNode)
  140. {
  141. return((PLIST_NODE) pNode->pNext);
  142. }
  143. void *
  144. LIST_GetElement(PLIST_NODE pNode)
  145. {
  146. return(pNode->pElement);
  147. }