Source code of Windows XP (NT5)
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.

326 lines
6.9 KiB

  1. #ifndef __SIMLIST_H_INCLUDED
  2. #define __SIMLIST_H_INCLUDED
  3. template <class T>
  4. class CSimpleLinkedList
  5. {
  6. private:
  7. class CLinkedListNode
  8. {
  9. private:
  10. CLinkedListNode *m_pNext;
  11. T m_Data;
  12. public:
  13. CLinkedListNode( const T &data )
  14. : m_pNext(NULL), m_Data(data)
  15. {
  16. }
  17. const CLinkedListNode *Next(void) const
  18. {
  19. return (m_pNext);
  20. }
  21. CLinkedListNode *Next(void)
  22. {
  23. return (m_pNext);
  24. }
  25. void Next( CLinkedListNode *pNext )
  26. {
  27. m_pNext = pNext;
  28. }
  29. const T &Data(void) const
  30. {
  31. return (m_Data);
  32. }
  33. T &Data(void)
  34. {
  35. return (m_Data);
  36. }
  37. };
  38. CLinkedListNode *m_pHead, *m_pTail;
  39. int m_nItemCount;
  40. public:
  41. CSimpleLinkedList( const CSimpleLinkedList &other )
  42. : m_pHead(NULL),m_pTail(NULL),m_nItemCount(0)
  43. {
  44. for (Iterator i(other);i != other.End();i++)
  45. Append(*i);
  46. }
  47. CSimpleLinkedList(void)
  48. : m_pHead(NULL),m_pTail(NULL),m_nItemCount(0)
  49. {
  50. }
  51. CSimpleLinkedList &operator=( const CSimpleLinkedList &other )
  52. {
  53. if (this != &other)
  54. {
  55. Destroy();
  56. for (Iterator i(other);i != other.End();i++)
  57. Append(*i);
  58. }
  59. return *this;
  60. }
  61. virtual ~CSimpleLinkedList(void)
  62. {
  63. Destroy();
  64. }
  65. void Destroy(void)
  66. {
  67. while (m_pHead)
  68. {
  69. CLinkedListNode *pCurr = m_pHead;
  70. m_pHead = m_pHead->Next();
  71. delete pCurr;
  72. }
  73. m_pHead = m_pTail = NULL;
  74. m_nItemCount = 0;
  75. }
  76. void Remove( const T &data )
  77. {
  78. CLinkedListNode *pPrev = NULL, *pCurr = m_pHead;
  79. while (pCurr && pCurr->Data() != data)
  80. {
  81. pPrev = pCurr;
  82. pCurr = pCurr->Next();
  83. }
  84. if (!pCurr)
  85. return;
  86. if (!pPrev)
  87. {
  88. m_pHead = pCurr->Next();
  89. delete pCurr;
  90. m_nItemCount--;
  91. }
  92. else
  93. {
  94. pPrev->Next(pCurr->Next());
  95. delete pCurr;
  96. m_nItemCount--;
  97. }
  98. }
  99. void Append( const CSimpleLinkedList &other )
  100. {
  101. for (Iterator i(other);i != other.End();i++)
  102. Append(*i);
  103. }
  104. int Count(void) const
  105. {
  106. return m_nItemCount;
  107. }
  108. class Iterator;
  109. friend class Iterator;
  110. class Iterator
  111. {
  112. private:
  113. CLinkedListNode *m_pCurr;
  114. public:
  115. Iterator( CLinkedListNode *pNode )
  116. : m_pCurr(pNode)
  117. {
  118. }
  119. Iterator( const CSimpleLinkedList &list )
  120. : m_pCurr(list.m_pHead)
  121. {
  122. }
  123. Iterator(void)
  124. : m_pCurr(NULL)
  125. {
  126. }
  127. Iterator &Next(void)
  128. {
  129. if (m_pCurr)
  130. m_pCurr = m_pCurr->Next();
  131. return (*this);
  132. }
  133. Iterator &Begin(const CSimpleLinkedList &list)
  134. {
  135. m_pCurr = list.m_pHead;
  136. return (*this);
  137. }
  138. Iterator &operator=( const Iterator &other )
  139. {
  140. m_pCurr = other.m_pCurr;
  141. return (*this);
  142. }
  143. bool End(void) const
  144. {
  145. return(m_pCurr == NULL);
  146. }
  147. T &operator*(void)
  148. {
  149. return (m_pCurr->Data());
  150. }
  151. const T &operator*(void) const
  152. {
  153. return (m_pCurr->Data());
  154. }
  155. Iterator &operator++(void)
  156. {
  157. Next();
  158. return (*this);
  159. }
  160. Iterator operator++(int)
  161. {
  162. Iterator tmp(*this);
  163. Next();
  164. return (tmp);
  165. }
  166. bool operator!=( const Iterator &other ) const
  167. {
  168. return (m_pCurr != other.m_pCurr);
  169. }
  170. bool operator==( const Iterator &other ) const
  171. {
  172. return (m_pCurr == other.m_pCurr);
  173. }
  174. };
  175. Iterator Begin(void) const
  176. {
  177. return Iterator(*this);
  178. }
  179. Iterator End(void) const
  180. {
  181. return Iterator();
  182. }
  183. Iterator Begin(void)
  184. {
  185. return Iterator(*this);
  186. }
  187. Iterator End(void)
  188. {
  189. return Iterator();
  190. }
  191. Iterator Find( const T &data )
  192. {
  193. for (Iterator i=Begin();i != End();++i)
  194. if (*i == data)
  195. return i;
  196. return End();
  197. }
  198. Iterator Insert( Iterator &nextItem, const T &data )
  199. {
  200. CLinkedListNode *pNewItem = new CLinkedListNode(data);
  201. if (pNewItem)
  202. {
  203. if (Empty())
  204. {
  205. m_pHead = m_pTail = pNewItem;
  206. }
  207. else if (nextItem.Data() == m_pHead)
  208. {
  209. pNewItem->Next(m_pHead);
  210. m_pHead = pNewItem;
  211. }
  212. else if (nextItem.Data() == NULL)
  213. {
  214. m_pTail->Next(pNewItem);
  215. m_pTail = pNewItem;
  216. }
  217. m_nItemCount++;
  218. }
  219. }
  220. Iterator Prepend( const T &data )
  221. {
  222. CLinkedListNode *pNewItem = new CLinkedListNode(data);
  223. if (pNewItem)
  224. {
  225. if (Empty())
  226. {
  227. m_pHead = m_pTail = pNewItem;
  228. }
  229. else
  230. {
  231. pNewItem->Next(m_pHead);
  232. m_pHead = pNewItem;
  233. }
  234. m_nItemCount++;
  235. }
  236. return Iterator(pNewItem);
  237. }
  238. Iterator Append( const T &data )
  239. {
  240. CLinkedListNode *pNewItem = new CLinkedListNode(data);
  241. if (pNewItem)
  242. {
  243. if (Empty())
  244. {
  245. m_pHead = m_pTail = pNewItem;
  246. }
  247. else
  248. {
  249. m_pTail->Next(pNewItem);
  250. m_pTail = pNewItem;
  251. }
  252. m_nItemCount++;
  253. }
  254. return Iterator(pNewItem);
  255. }
  256. bool Empty(void) const
  257. {
  258. return (m_pHead == NULL);
  259. }
  260. };
  261. template <class T>
  262. class CSimpleStack : public CSimpleLinkedList<T>
  263. {
  264. private:
  265. CSimpleStack( const CSimpleStack &other );
  266. CSimpleStack &operator=( const CSimpleStack &other );
  267. public:
  268. CSimpleStack(void)
  269. {
  270. }
  271. virtual ~CSimpleStack(void)
  272. {
  273. }
  274. void Push( const T &data )
  275. {
  276. Prepend(data);
  277. }
  278. bool Pop( T &data )
  279. {
  280. if (Empty())
  281. return false;
  282. Iterator iter(*this);
  283. data = *iter;
  284. Remove(*iter);
  285. return true;
  286. }
  287. };
  288. template <class T>
  289. class CSimpleQueue : public CSimpleLinkedList<T>
  290. {
  291. private:
  292. CSimpleQueue( const CSimpleQueue &other );
  293. CSimpleQueue &operator=( const CSimpleQueue &other );
  294. public:
  295. CSimpleQueue(void)
  296. {
  297. }
  298. virtual ~CSimpleQueue(void)
  299. {
  300. }
  301. void Enqueue( const T &data )
  302. {
  303. Append(data);
  304. }
  305. bool Dequeue( T &data )
  306. {
  307. if (Empty())
  308. return false;
  309. Iterator iter(*this);
  310. data = *iter;
  311. Remove(*iter);
  312. return true;
  313. }
  314. };
  315. #endif __SIMLIST_H_INCLUDED