Guest User

Repl.it Solution

a guest
Dec 9th, 2019
134
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // LinkedBag340.cpp
  2. // Created by: CSC340
  3.  
  4. #include<iostream>
  5. #include<cstddef>
  6. #include<ctime>
  7. #include<cstdlib>
  8. #include"LinkedBag.h"
  9.  
  10.  
  11.  
  12. // removeSecondNode340()
  13. template<typename ItemType>
  14. bool LinkedBag<ItemType>::removeSecondNode340()
  15. {
  16. Node<ItemType>* nodeToDeletePtr = headPtr->getNext();
  17. return remove(nodeToDeletePtr->getItem()); // remove the second item
  18. }
  19.  
  20. // addEnd340()
  21. template<typename ItemType>
  22. bool LinkedBag<ItemType>::addEnd340(const ItemType& newEntry)
  23. {
  24. Node<ItemType> *curPtr = headPtr; // start from the head
  25.  
  26. while (curPtr->getNext() != nullptr)
  27. {
  28. curPtr = curPtr->getNext(); //use while loop to find the last node
  29. }
  30.  
  31. Node<ItemType> *newNodePtr = new Node<ItemType>(); //create a new node
  32. newNodePtr->setItem(newEntry);
  33. curPtr->setNext(newNodePtr); //insert to the end
  34. itemCount++;
  35. return true;
  36. }
  37.  
  38. // getCurrentSize340Iterative()
  39. template<typename ItemType>
  40. int LinkedBag<ItemType>::getCurrentSize340Iterative() const
  41. {
  42. int count=0;
  43. Node<ItemType> *curPtr = headPtr;
  44.  
  45. while (curPtr!= nullptr)
  46. {
  47. curPtr = curPtr->getNext(); // move to the next node
  48. count ++; //count until curPtr= headPtr
  49. }
  50. return count;
  51. }
  52.  
  53.  
  54. // getCurrentSize340Recursive()
  55. template<typename ItemType>
  56. int LinkedBag<ItemType>::getCurrentSize340Recursive() const
  57. {
  58. static Node<ItemType> *curPtr = headPtr;
  59.  
  60. if (curPtr == nullptr)
  61. {
  62. curPtr = headPtr;
  63. return 0;
  64. }
  65. else
  66. {
  67. curPtr = curPtr->getNext();
  68. return (getCurrentSize340RecursiveHelper(curPtr) ); //to function getCurrentSize340RecursiveHelper()
  69. }
  70.  
  71. }
  72.  
  73. // getCurrentSize340RecursiveHelper()
  74. template<typename ItemType>
  75. int LinkedBag<ItemType>::getCurrentSize340RecursiveHelper(Node<ItemType>*) const
  76. {
  77. Node<ItemType> *curPtr = headPtr;
  78. int count = 0;
  79. if (curPtr == nullptr)
  80. {
  81. curPtr = headPtr;
  82. return 0;
  83. }
  84. else
  85. {
  86. curPtr = curPtr->getNext();
  87. count++;
  88. return (count + getCurrentSize340Recursive() ); // back to getCurrentSize340Recursive()
  89. }
  90. }
  91.  
  92.  
  93. // getCurrentSize340RecursiveNoHelper()
  94. template<typename ItemType>
  95. int LinkedBag<ItemType>::getCurrentSize340RecursiveNoHelper() const
  96. {
  97. static Node<ItemType>* curPtr = headPtr;
  98. int count = 0;
  99.  
  100. if (curPtr == nullptr)
  101. {
  102. curPtr = headPtr;
  103. return 0;
  104. }
  105. else
  106. {
  107. curPtr = curPtr->getNext();
  108. count++;
  109. return (count + getCurrentSize340RecursiveNoHelper() );
  110. }
  111.  
  112. }
  113.  
  114.  
  115.  
  116. // getFrequencyOf340Recursive()
  117. template<typename ItemType>
  118. int LinkedBag<ItemType>::getFrequencyOf340Recursive(const ItemType& anEntry) const
  119. {
  120. static Node<ItemType> *curPtr = headPtr;
  121.  
  122. if (curPtr == nullptr)
  123. {
  124. curPtr = headPtr;
  125. return 0;
  126. }
  127. else
  128. return (getFrequencyOf340RecursiveHelper(curPtr,anEntry) );
  129.  
  130. }
  131.  
  132. // getFrequencyOf340RecursiveHelper()
  133. template<typename ItemType>
  134. int LinkedBag<ItemType>::getFrequencyOf340RecursiveHelper(Node<ItemType>*, const ItemType& anEntry) const
  135. {
  136. static Node<ItemType> *curPtr = headPtr;
  137. int count = 0;
  138.  
  139. if (curPtr == nullptr)
  140. {
  141. curPtr = headPtr;
  142. return 0;
  143. }
  144. else if (curPtr->getItem() == anEntry)
  145. {
  146. curPtr = curPtr->getNext();
  147. count++;
  148. return (count + getFrequencyOf340Recursive(anEntry) );
  149. }
  150. else
  151. {
  152. curPtr = curPtr->getNext();
  153. return (count + getFrequencyOf340Recursive(anEntry));
  154. }
  155.  
  156. }
  157.  
  158. // getFrequencyOf340RecursiveNoHelper()
  159. template<typename ItemType>
  160. int LinkedBag<ItemType>::getFrequencyOf340RecursiveNoHelper(const ItemType& anEntry) const
  161. {
  162. static Node<ItemType>* curPtr = headPtr;
  163. int count = 0;
  164.  
  165. if (curPtr == nullptr)
  166. {
  167. curPtr = headPtr;
  168. return 0;
  169. }
  170. else if (curPtr->getItem() == anEntry)
  171. {
  172. curPtr = curPtr->getNext();
  173. count++;
  174. return (count + getFrequencyOf340RecursiveNoHelper(anEntry) );
  175. }
  176. else
  177. {
  178. curPtr = curPtr->getNext();
  179. return (count + getFrequencyOf340RecursiveNoHelper(anEntry));
  180. }
  181.  
  182.  
  183. }
  184.  
  185.  
  186.  
  187. //#8 removeRandom340()
  188. /*template<typename ItemType>
  189. ItemType LinkedBag<ItemType>::removeRandom340()
  190. {
  191.  
  192. int random_num{};
  193. int size = getCurrentSize340Iterative(); // number of random numbers to generate
  194. int min{ 1 }; // lower bound (inclusive)
  195. int max = size; // upper bound (inclusive)
  196. ItemType item{};
  197.  
  198. srand(time(nullptr)); // seed the random number
  199.  
  200. for (int i{ 1 }; i <= max; i++)
  201. {
  202. random_num = rand() % max + min; // generate a random number [min, max]
  203.  
  204. }
  205.  
  206. Node<ItemType>* temp1 = headPtr; // start from the head
  207. int position = random_num;
  208.  
  209. for (int i{ 0 }; i < (position - 2); i++)
  210. { // traverse through the node
  211. temp1 = temp1->getNext(); // temp1 points to (n-1)th Node
  212. }
  213. Node<ItemType>* temp2 = temp1->getNext(); // nth Node
  214. item = temp2->getItem();
  215. remove(item);
  216. return item;
  217. } // end removeRandom340()*/
  218.  
  219. //removeRandom340 is not working, to make the whole program work, have to hide it
  220. /*
  221. template<typename ItemType>
  222. ItemType LinkedBag<ItemType>::removeRandom340()
  223. {
  224.  
  225. Node<ItemType>* curPtr = headPtr;
  226. if(curPtr == nullptr )
  227. return 0;
  228.  
  229. srand(time(NULL));
  230.  
  231. int n;
  232. for (n=2; curPtr!=NULL; n++)
  233. {
  234. if (rand()%n == 0)
  235.  
  236. curPtr=curPtr->getNext();
  237. }
  238. Node<ItemType>* node = curPtr->getNext();
  239.  
  240. remove(node->getNext());
  241. }
  242. */
RAW Paste Data