LinkedList.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // Project: Singly_Linked_List.cbp
  2. // File : LinkedList.h
  3. #ifndef LINKEDLIST_H
  4. #define LINKEDLIST_H
  5. #include <iostream>
  6. #include "Node.h"
  7. template <typename T>
  8. class LinkedList
  9. {
  10. private:
  11. int m_count;
  12. public:
  13. // The first node in the list
  14. // or null if empty
  15. Node<T> * Head;
  16. // The last node in the list
  17. // or null if empty
  18. Node<T> * Tail;
  19. // Constructor
  20. LinkedList();
  21. // Get() operation
  22. Node<T> * Get(int index);
  23. // Insert() operation
  24. void InsertHead(T val);
  25. void InsertTail(T val);
  26. void Insert(int index, T val);
  27. // Search() operation
  28. int Search(T val);
  29. // Remove() operation
  30. void RemoveHead();
  31. void RemoveTail();
  32. void Remove(int index);
  33. // Additional operation
  34. int Count();
  35. void PrintList();
  36. };
  37. template <typename T>
  38. LinkedList<T>::LinkedList() : m_count(0), Head(NULL), Tail(NULL) {}
  39. template <typename T>
  40. Node<T> * LinkedList<T>::Get(int index)
  41. {
  42. // Check if the index is out of bound
  43. if(index < 0 || index > m_count)
  44. return NULL;
  45. // Start from the Head
  46. Node<T> * node = Head;
  47. // Iterate through the linked list elements
  48. // until it finds the selected index
  49. for(int i = 0; i < index; ++i)
  50. {
  51. node = node->Next;
  52. }
  53. // Simply return the node result
  54. return node;
  55. }
  56. template <typename T>
  57. void LinkedList<T>::InsertHead(T val)
  58. {
  59. // Create a new Node
  60. Node<T> * node = new Node<T>(val);
  61. // The current Head will no longer become a Head
  62. // so the Next pointer of the new Node will
  63. // point to the current Head
  64. node->Next = Head;
  65. // The new Node now become the Head
  66. Head = node;
  67. // If the linked list is empty
  68. // The Tail is also the Head
  69. if(m_count == 0)
  70. Tail = Head;
  71. // One element is added
  72. m_count++;
  73. }
  74. template <typename T>
  75. void LinkedList<T>::InsertTail(T val)
  76. {
  77. // If the linked list is empty,
  78. // just simply invoke InsertHead()
  79. if(m_count == 0)
  80. {
  81. InsertHead(val);
  82. return;
  83. }
  84. // Create a new Node
  85. Node<T> * node = new Node<T>(val);
  86. // The current Tail will no longer become a Tail
  87. // so the Next pointer of the current Tail will
  88. // point to the new node
  89. Tail->Next = node;
  90. // The new Node now become the Tail
  91. Tail = node;
  92. // One element is added
  93. m_count++;
  94. }
  95. template <typename T>
  96. void LinkedList<T>::Insert(int index, T val)
  97. {
  98. // Check if the index is out of bound
  99. if(index < 0 || index > m_count)
  100. return;
  101. // If inserting a new Head
  102. if(index == 0)
  103. {
  104. InsertHead(val);
  105. return;
  106. }
  107. // If inserting a new Tail
  108. else if(index == m_count)
  109. {
  110. InsertTail(val);
  111. return;
  112. }
  113. // Start to find previous node
  114. // from the Head
  115. Node<T> * prevNode = Head;
  116. // Traverse the elements until
  117. // the selected index is found
  118. for(int i = 0; i < index - 1; ++i)
  119. {
  120. prevNode = prevNode->Next;
  121. }
  122. // Create the next node which is
  123. // the element after previous node
  124. Node<T> * nextNode = prevNode->Next;
  125. // Create a new node
  126. Node<T> * node = new Node<T>(val);
  127. // Insert this new node between
  128. // the previous node and the next node
  129. node->Next = nextNode;
  130. prevNode->Next = node;
  131. // One element is added
  132. m_count++;
  133. }
  134. template <typename T>
  135. int LinkedList<T>::Search(T val)
  136. {
  137. // If LinkedList is empty,
  138. // just return NOT_FOUND
  139. if(m_count == 0)
  140. return -1;
  141. // Need to count the index
  142. int index = 0;
  143. // Traverse from the Head node
  144. Node<T> * node = Head;
  145. // Traverse until the selected value
  146. // is matched with the value
  147. // of the current position,
  148. while(node->Value != val)
  149. {
  150. index++;
  151. node = node->Next;
  152. // This will happen
  153. // if the selected value
  154. // is not found
  155. if(node == NULL)
  156. {
  157. return -1;
  158. }
  159. }
  160. return index;
  161. }
  162. template <typename T>
  163. void LinkedList<T>::RemoveHead()
  164. {
  165. // Do nothing if list is empty
  166. if(m_count == 0)
  167. return;
  168. // Save the current Head
  169. // to a new node
  170. Node<T> * node = Head;
  171. // Point the Head Pointer
  172. // to the element next to the current Head
  173. Head = Head->Next;
  174. // Now it's safe to remove
  175. // the first element
  176. delete node;
  177. // One element is removed
  178. m_count--;
  179. }
  180. template <typename T>
  181. void LinkedList<T>::RemoveTail()
  182. {
  183. // Do nothing if list is empty
  184. if(m_count == 0)
  185. return;
  186. // If List element is only one
  187. // just simply call RemoveHead()
  188. if(m_count == 1)
  189. {
  190. RemoveHead();
  191. return;
  192. }
  193. // Start to find previous node
  194. // from the Head
  195. Node<T> * prevNode = Head;
  196. // This is the candidate of
  197. // removed items which is
  198. // the element next to the prevNode
  199. Node<T> * node = Head->Next;
  200. // Traverse the elements until
  201. // the last element
  202. while(node->Next != NULL)
  203. {
  204. prevNode = prevNode->Next;
  205. node = node->Next;
  206. }
  207. // the prevNode now becomes the Tail
  208. // so the Next pointer of the prevNode
  209. // point to NULL
  210. prevNode->Next = NULL;
  211. Tail = prevNode;
  212. // Now it's safe to remove
  213. // the last element
  214. delete node;
  215. // One element is removed
  216. m_count--;
  217. }
  218. template <typename T>
  219. void LinkedList<T>::Remove(int index)
  220. {
  221. // Do nothing if list is empty
  222. if(m_count == 0)
  223. return;
  224. // Do nothing if index is out of bound
  225. if(index < 0 || index >= m_count)
  226. return;
  227. // If removing the current Head
  228. if(index == 0)
  229. {
  230. RemoveHead();
  231. return;
  232. }
  233. // If removing the current Tail
  234. else if(index == m_count - 1)
  235. {
  236. RemoveTail();
  237. return;
  238. }
  239. // Start to traverse the list
  240. // from the Head
  241. Node<T> * prevNode = Head;
  242. // Find the element before
  243. // the selected index
  244. for(int i = 0; i < index - 1; ++i)
  245. {
  246. prevNode = prevNode->Next;
  247. }
  248. // The removed element is after
  249. // the prevNode
  250. Node<T> * node = prevNode->Next;
  251. // The nextNode will be the neighbor of
  252. // prevNode if the node is removed
  253. Node<T> * nextNode = node->Next;
  254. // Link the prevNode to nextNode
  255. prevNode->Next = nextNode;
  256. // It's now safe to remove
  257. // the selected index element
  258. delete node;
  259. // One element is removed
  260. m_count--;
  261. }
  262. template <typename T>
  263. int LinkedList<T>::Count()
  264. {
  265. return m_count;
  266. }
  267. template <typename T>
  268. void LinkedList<T>::PrintList()
  269. {
  270. Node<T> * node = Head;
  271. while(node != NULL)
  272. {
  273. std::cout << node->Value << " -> ";
  274. node = node->Next;
  275. }
  276. std::cout << "NULL" << std::endl;
  277. }
  278. #endif // LINKEDLIST_H