Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.94 KB | None | 0 0
  1. //*******************************************************************************************************
  2. //
  3. // File: LinkedList.h
  4. //
  5. // Student: Georgy Sinitsyn
  6. //
  7. // Assignment: Assignment #14: StudentList
  8. //
  9. // Course name: Data Structures I
  10. //
  11. // Course number: COSC 3050 - 01
  12. //
  13. // Due: October 10, 2016
  14. //
  15. //
  16. // This header contains the LinkedList class with all functions
  17. //
  18. // Other files required:
  19. // 1. LLdriver.cpp
  20. // 2. Node.h
  21. //
  22. //*******************************************************************************************************
  23.  
  24. #ifndef DOUBLYLINKEDLIST_H
  25. #define DOUBLYLINKEDLIST_H
  26.  
  27. #include "Node.h"
  28. #include <iostream>
  29. using namespace std;
  30.  
  31. template <typename Type>
  32. class DLList
  33. {
  34. private:
  35. Node <Type> *front;
  36. Node <Type> *rear;
  37. public:
  38. DLList();
  39. ~DLList();
  40. bool insert(const Type &);
  41. bool remove(Type &);
  42. bool retrieve(Type &) const;
  43. bool viewFront(Type &) const;
  44. bool viewBack(Type &) const;
  45. bool isEmpty() const;
  46. bool isFull() const;
  47. bool clearList();
  48. void display() const;
  49. void displayReverse() const;
  50. int getSize() const;
  51. bool counting(const Type &, int &) const;
  52. bool removeAllBut(const Type &);
  53. };
  54.  
  55. //*******************************************************************************************************
  56. // Constructor
  57. //*******************************************************************************************************
  58.  
  59. template <typename Type>
  60. DLList <Type> ::DLList()
  61. {
  62. front = nullptr;
  63. rear = nullptr;
  64. }
  65.  
  66. //*******************************************************************************************************
  67. // Destructor
  68. //*******************************************************************************************************
  69.  
  70. template <typename Type>
  71. DLList <Type> :: ~DLList()
  72. {
  73. Node <Type> *pTemp;
  74. rear = nullptr;
  75. while (front != nullptr)
  76. {
  77. pTemp = front;
  78. front = pTemp->next;
  79. delete pTemp;
  80. }
  81. }
  82.  
  83. //*******************************************************************************************************
  84. // insert() function
  85. // insert a new node into the linkedlist
  86. //*******************************************************************************************************
  87.  
  88. template <typename Type>
  89. bool DLList <Type> ::insert(const Type &dataIn)
  90. {
  91. bool success = false;
  92.  
  93. Node <Type> *pTemp = front;
  94.  
  95. while (pTemp != nullptr && pTemp->data < dataIn)
  96. pTemp = pTemp->next;
  97.  
  98. Node <Type> *pNew = new (nothrow) Node <Type>(dataIn, pTemp, nullptr);
  99.  
  100. if (pNew != nullptr)
  101. {
  102. if (front == nullptr)
  103. {
  104. rear = pNew;
  105. front = pNew;
  106. }
  107. else if (pTemp == front)
  108. {
  109. front->prev = pNew;
  110. front = pNew;
  111. }
  112. else if (pTemp == nullptr)
  113. {
  114. rear->next = pNew;
  115. pNew->prev = rear;
  116. rear = pNew;
  117. }
  118. else
  119. {
  120. pNew->prev = pTemp->prev;
  121. pTemp->prev->next = pNew;
  122. pTemp->prev = pNew;
  123. }
  124. success = true;
  125. }
  126.  
  127. return success;
  128. }
  129.  
  130. //*******************************************************************************************************
  131. // remove() function
  132. // removes a node from the linkedlist
  133. //*******************************************************************************************************
  134.  
  135. template <typename Type>
  136. bool DLList <Type> ::remove(Type &dataOut)
  137. {
  138. bool success = false;
  139.  
  140. Node <Type> *pDel = front;
  141.  
  142. while (pDel != nullptr && pDel->data < dataOut)
  143. pDel = pDel->next;
  144.  
  145. if (pDel != nullptr && pDel->data == dataOut)
  146. {
  147. dataOut = pDel->data;
  148. if (front->next == nullptr)
  149. {
  150. rear = nullptr;
  151. front = nullptr;
  152. }
  153. else if (pDel->prev == nullptr)
  154. front = front->next;
  155. else if (pDel->next == nullptr)
  156. rear = rear->prev;
  157. else
  158. {
  159. pDel->prev->next = pDel->next;
  160. pDel->next->prev = pDel->prev;
  161. }
  162.  
  163. delete pDel;
  164. success = true;
  165. }
  166.  
  167. return success;
  168. }
  169.  
  170. //*******************************************************************************************************
  171. // retrieve() function
  172. // retrieves a node from the linkedlist
  173. //*******************************************************************************************************
  174.  
  175. template <typename Type>
  176. bool DLList <Type> ::retrieve(Type &dataOut) const
  177. {
  178. bool success = false;
  179.  
  180. Node <Type> *pTemp = front;
  181.  
  182. while (pTemp != nullptr && pTemp->data < dataOut)
  183. pTemp = pTemp->next;
  184.  
  185. if (pTemp != nullptr && pTemp->data == dataOut)
  186. {
  187. dataOut = pTemp->data;
  188. success = true;
  189. }
  190.  
  191. return success;
  192. }
  193.  
  194. //*******************************************************************************************************
  195. // viewFront() function
  196. // view the first node in the linkedlist
  197. //*******************************************************************************************************
  198.  
  199. template <typename Type>
  200. bool DLList <Type> ::viewFront(Type &dataOut) const
  201. {
  202. bool success = false;
  203.  
  204. if (front != nullptr)
  205. {
  206. dataOut = front->data;
  207. success = true;
  208. }
  209.  
  210. return success;
  211. }
  212.  
  213. //*******************************************************************************************************
  214. // viewBack() function
  215. // view the last node in the linkedlist
  216. //*******************************************************************************************************
  217.  
  218. template <typename Type>
  219. bool DLList <Type> ::viewBack(Type &dataOut) const
  220. {
  221. bool success = false;
  222.  
  223. if (front != nullptr)
  224. {
  225. dataOut = rear->data;
  226. success = true;
  227. }
  228.  
  229. return success;
  230. }
  231.  
  232. //*******************************************************************************************************
  233. // isEmpty() function
  234. // checks if the linkedlist is empty
  235. //*******************************************************************************************************
  236.  
  237. template <typename Type>
  238. bool DLList <Type> ::isEmpty() const
  239. {
  240. bool success = false;
  241.  
  242. if (front == nullptr)
  243. success = true;
  244.  
  245. return success;
  246. }
  247.  
  248. //*******************************************************************************************************
  249. // isFull() function
  250. // checks if the linkedlist is full
  251. //*******************************************************************************************************
  252.  
  253. template <typename Type>
  254. bool DLList <Type> ::isFull() const
  255. {
  256. bool full = true;
  257. Node <Type> *pTemp = new (nothrow)Node <Type>;
  258. if (pTemp != nullptr)
  259. {
  260. full = false;
  261. delete pTemp;
  262. }
  263. return full;
  264.  
  265. }
  266.  
  267. //*******************************************************************************************************
  268. // clearList() function
  269. // clears the list from every node
  270. //*******************************************************************************************************
  271.  
  272. template <typename Type>
  273. bool DLList <Type> ::clearList()
  274. {
  275. bool success = false;
  276.  
  277. Node <Type> *pTemp;
  278. rear = nullptr;
  279. if (front != nullptr)
  280. {
  281. while (front != nullptr)
  282. {
  283. pTemp = front;
  284. front = pTemp->next;
  285. delete pTemp;
  286. }
  287. success = true;
  288. }
  289.  
  290. return success;
  291. }
  292.  
  293. //*******************************************************************************************************
  294. // display() function
  295. // displays every node in the console
  296. //*******************************************************************************************************
  297.  
  298. template <typename Type>
  299. void DLList <Type> ::display() const
  300. {
  301. Node <Type> *pTemp = front;
  302.  
  303. while (pTemp != nullptr)
  304. {
  305. cout << pTemp->data;
  306. pTemp = pTemp->next;
  307. }
  308. }
  309.  
  310. //*******************************************************************************************************
  311. // display() function
  312. // displays every node in reversed way in the console
  313. //*******************************************************************************************************
  314.  
  315. template <typename Type>
  316. void DLList <Type> ::displayReverse() const
  317. {
  318. Node <Type> *pTemp = rear;
  319. while (pTemp != nullptr)
  320. {
  321. cout << pTemp->data;
  322. pTemp = pTemp->prev;
  323. }
  324.  
  325. }
  326.  
  327. //*******************************************************************************************************
  328. // getSize() function
  329. // gets the number of nodes in the linkedlist
  330. //*******************************************************************************************************
  331.  
  332. template <typename Type>
  333. int DLList <Type> ::getSize() const
  334. {
  335. Node <Type> *pTemp = front;
  336. int num = 0;
  337.  
  338. while (pTemp != nullptr)
  339. {
  340. num++;
  341. pTemp = pTemp->next;
  342. }
  343.  
  344. return num;
  345. }
  346.  
  347. //*******************************************************************************************************
  348. // counting() function
  349. // gets the number of specific nodes
  350. //*******************************************************************************************************
  351.  
  352. template <typename Type>
  353. bool DLList <Type> ::counting(const Type &dataIn, int &count) const
  354. {
  355. Node <Type> *pTemp = front;
  356. bool success = false;
  357.  
  358. count = 0;
  359.  
  360. if (pTemp != nullptr)
  361. {
  362. while (pTemp != nullptr)
  363. {
  364. if (pTemp->data == dataIn)
  365. count++;
  366. pTemp = pTemp->next;
  367. }
  368. success = true;
  369. }
  370.  
  371. return success;
  372. }
  373.  
  374. //*******************************************************************************************************
  375. // removeAllBut() function
  376. // remove all nodes except for a chosen one
  377. //*******************************************************************************************************
  378.  
  379. template <typename Type>
  380. bool DLList <Type> ::removeAllBut(const Type &dataIn)
  381. {
  382. bool success = false;
  383.  
  384. Node <Type> *pTemp = front;
  385. Node <Type> *pDel = nullptr;
  386.  
  387. if (pTemp != nullptr)
  388. {
  389. while (pTemp != nullptr)
  390. {
  391. if (pTemp->data != dataIn)
  392. {
  393. pDel = pTemp;
  394. if (rear = pDel && front == pDel)
  395. {
  396. front = nullptr;
  397. rear = nullptr;
  398. }
  399. else if (front == pDel)
  400. {
  401. front = front->next;
  402. pTemp = front;
  403. }
  404. else if (rear == pDel)
  405. {
  406. rear = rear->prev;
  407. }
  408. else
  409. {
  410. pTemp->prev->next = pTemp->next;
  411. pTemp->next->prev = pTemp->prev;
  412. }
  413. delete pDel;
  414. }
  415. else
  416. pTemp = pTemp->next;
  417. }
  418. success = true;
  419. }
  420. return success;
  421. }
  422.  
  423. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement