llist.cpp
上传用户:loctite114
上传日期:2007-01-03
资源大小:49k
文件大小:5k
源码类别:

棋牌游戏

开发平台:

Visual C++

  1. //*******************
  2. //*
  3. //* Filename: llist.cpp
  4. //*
  5. //* Notes: 
  6. //*
  7. //* Linked list class for handing any type of object.
  8. //*
  9. //***************************
  10. //*
  11. //* Date Created: 05/14/98
  12. //* Author: Lee Patterson
  13. //*
  14. //* Copyright (c) 1999, Lee Patterson
  15. //*
  16. //*************************************
  17. #include <stdlib.h>
  18. #include <malloc.h>
  19. #include "llist.h"
  20. //uncomment the following line if this will be compiled using a windows mfc application
  21. //used only when debugging
  22. //#define WINDEBUGGING
  23. #ifdef WINDEBUGGING
  24. #include "windows.h"
  25. #include <stdio.h>
  26. #endif
  27. CLList::CLList()
  28. {
  29. m_iNumEntries = 0;
  30. m_uniqueID=1;
  31. m_pHead = NULL;
  32. m_pTail = NULL;
  33. m_pNext = NULL;
  34. m_pPrev = NULL;
  35. }  
  36. CLList::~CLList()
  37. {
  38. RemoveAll();
  39. }
  40. void CLList::RemoveAll()
  41. {
  42. if(m_iNumEntries)
  43. {
  44. CLList* pList = GetHead();
  45. CLList* pNext;
  46. while (pList)
  47. {
  48. m_iNumEntries--;
  49. pNext = pList->m_pNext;
  50. //if(m_iNumEntries>0) //this line removed as a remove all wouldn't remove the last item.
  51. delete pList;
  52. pList = pNext;
  53. }
  54. m_pHead=NULL;
  55. m_pTail=NULL;
  56. m_iNumEntries=0;
  57. }
  58. }
  59. void CLList::RemoveAllItems()
  60. {
  61. if(m_iNumEntries)
  62. {
  63. CLList* pList = GetHead();
  64. CLList* pNext;
  65. while (pList)
  66. {
  67. m_iNumEntries--;
  68. pNext = pList->m_pNext;
  69. delete pList->m_pvItem;
  70. //if(m_iNumEntries>0)
  71. delete pList;
  72. pList = pNext;
  73. }
  74. m_pHead=NULL;
  75. m_pTail=NULL;
  76. m_iNumEntries=0;
  77. }
  78. }
  79. void CLList::AddHead (void* pvItem)
  80. {
  81. CLList *pHead = GetHead ();
  82. CLList *pItem = new CLList;
  83. #ifdef WINDEBUGGING
  84. char buf[80];
  85. sprintf(buf,"CLList::AddHead new %Xn",pItem);
  86. OutputDebugString(buf);
  87. #endif
  88. pItem->m_pvItem = pvItem;
  89. pItem->m_id=m_uniqueID++;
  90. //setup next & prev for new item
  91. pItem->m_pPrev = NULL;
  92. pItem->m_pNext = pHead;
  93. m_pHead = pItem; //new head item
  94. if(!m_pTail)
  95. m_pTail=m_pHead; //LBP (06/02/99): new item, so tail has to be the same
  96. //setup next & prev for adjacent items
  97. if(pHead)
  98. pHead->m_pPrev = pItem;
  99. m_iNumEntries++;
  100. }
  101. void CLList::AddTail (void* pvItem)
  102. {
  103. CLList *pTail = GetTail ();
  104. CLList *pItem = new CLList;
  105. #ifdef WINDEBUGGING
  106. char buf[80];
  107. sprintf(buf,"CLList::AddTail new %Xn",pItem);
  108. OutputDebugString(buf);
  109. #endif
  110. pItem->m_pvItem = pvItem;
  111. pItem->m_id=m_uniqueID++;
  112. pItem->m_pPrev = pTail;
  113. if(!pTail)
  114. m_pHead=pItem;
  115. else
  116. pTail->m_pNext = pItem;
  117. m_pTail = pItem;
  118. m_iNumEntries++;
  119. }
  120. void CLList::RemoveItem (CLList* pList)
  121. {
  122. if(!pList)
  123. return;
  124. m_iNumEntries--;
  125. if(!m_iNumEntries)
  126. {
  127. m_pHead=NULL;
  128. m_pTail=NULL;
  129. m_pvItem=NULL;
  130. delete pList;
  131. return;
  132. }
  133. if (pList->m_pPrev)
  134. {
  135. pList->m_pPrev->m_pNext = pList->m_pNext;
  136. if (pList->m_pNext)
  137. pList->m_pNext->m_pPrev = pList->m_pPrev;
  138. else
  139. m_pTail=pList->m_pPrev; //this was the last item in the list
  140. }
  141. else 
  142. {
  143. m_pHead=pList->m_pNext; //this was the first item in the list
  144. if(pList->m_pNext)
  145. pList->m_pNext->m_pPrev = NULL;
  146. }
  147. delete pList;
  148. }
  149. void CLList::RemoveTail ()
  150. {
  151. CLList *pList = GetTail ();
  152. if(pList)
  153. {
  154. if(pList->m_pPrev)
  155. pList->m_pPrev->m_pNext=NULL;
  156. m_pTail=pList->m_pPrev;
  157. if(m_pTail==NULL)
  158. m_pHead=NULL; //nothing left on the list
  159. delete pList;
  160. m_iNumEntries--;
  161. }
  162. }
  163. void CLList::RemoveHead ()
  164. {
  165. CLList *pList = GetHead ();
  166. if(pList)
  167. {
  168. if(pList->m_pNext)
  169. pList->m_pNext->m_pPrev=NULL;
  170. m_pHead=pList->m_pNext;
  171. if(m_pHead==NULL)
  172. m_pTail=NULL; //nothing left on the list
  173. delete pList;
  174. m_iNumEntries--;
  175. }
  176. }
  177. void CLList::InsertAfter (CLList* pList, void* pvItem)
  178. {
  179. if (!pList->m_pNext)
  180. {
  181. AddTail (pvItem);
  182. }
  183. else
  184. {
  185. CLList* pItem = new CLList;
  186. pItem->m_pvItem = pvItem;
  187. pItem->m_id=m_uniqueID++;
  188. //setup next & prev for new item
  189. pItem->m_pPrev = pList;
  190. pItem->m_pNext = pList->m_pNext;
  191. //setup next & prev for adjacent items
  192. pList->m_pNext->m_pPrev = pItem;
  193. pList->m_pNext = pItem;
  194. m_iNumEntries++;
  195. }
  196. }
  197. void CLList::InsertBefore (CLList* pList, void* pvItem)
  198. {
  199. if (!pList->m_pPrev)
  200. {
  201. AddTail (pvItem);
  202. }
  203. else
  204. {
  205. CLList* pItem = new CLList;
  206. pItem->m_pvItem = pvItem;
  207. pItem->m_id=m_uniqueID++;
  208. //setup next & prev for new item
  209. pItem->m_pNext = pList;
  210. pItem->m_pPrev = pList->m_pPrev;
  211. //setup next & prev for adjacent items
  212. pList->m_pPrev->m_pNext = pItem;
  213. pList->m_pPrev = pItem;
  214. m_iNumEntries++;
  215. }
  216. }
  217. CLList* CLList::FindItem(void* pvItem)
  218. {
  219. CLList* pList = GetHead();
  220. while(pList)
  221. {
  222. if(pList->GetItem()==pvItem)
  223. return pList;
  224. pList=pList->GetNext();
  225. }
  226. return NULL;
  227. }
  228. CLList* CLList::FindID(int id)
  229. {
  230. CLList* pList = GetHead();
  231. while(pList)
  232. {
  233. if(pList->GetID()==id)
  234. return pList;
  235. pList=pList->GetNext();
  236. }
  237. return NULL;
  238. }
  239. CLList* CLList::GetIndex(int iIndex)
  240. {
  241. CLList* plist=GetHead();
  242. if(plist)
  243. for(int i=1; i<iIndex && plist; i++,plist=plist->GetNext()) {}
  244. return plist;
  245. }