Advertisement
Cinestra

Project 2 with more intuitive explanations

Mar 3rd, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.15 KB | None | 0 0
  1. #include "Set.h"
  2. #include <iostream>
  3.  
  4. void Set::initializeEmptySet()
  5. {
  6. m_size = 0;
  7.  
  8. m_head = new Node;
  9. m_tail = m_head;
  10. m_head->m_next = nullptr;
  11. m_head->m_prev = nullptr;
  12. m_tail->m_prev = nullptr;
  13. m_tail->m_next = nullptr;
  14. }
  15.  
  16. Set::Set()
  17. {
  18. initializeEmptySet();
  19. }
  20.  
  21. // Before we can actually insert the node, we need to implement the insert before function
  22. void Set::insertNewHead(const ItemType& value)
  23. {
  24.  
  25. Node* new_head = new Node;
  26. new_head->m_value = value;
  27.  
  28. m_head->m_prev = new_head;
  29. new_head->m_prev = nullptr;
  30. new_head->m_next = m_head;
  31. m_head = new_head;
  32.  
  33. m_size++;
  34. }
  35.  
  36. void Set::insertNewTail(const ItemType& value)
  37. {
  38. if (m_head == nullptr)
  39. insertNewHead(value);
  40.  
  41. else
  42. {
  43. Node* old_tail = m_tail;
  44.  
  45. Node* new_tail = new Node;
  46. old_tail->m_next = new_tail;
  47. new_tail->m_value = value;
  48. new_tail->m_prev = old_tail;
  49. new_tail->m_next = nullptr;
  50. m_tail = new_tail;
  51. }
  52.  
  53. m_size++;
  54. }
  55.  
  56. void Set::insertBefore(Node* p, const ItemType& value)
  57. {
  58. //Create a new node
  59.  
  60. Node* new_pointer = new Node;
  61. new_pointer->m_value = value;
  62.  
  63. if (p == m_head)
  64. {
  65. insertNewHead(value);
  66. return;
  67. }
  68.  
  69. // Set the new pointer's previous pointer equal to p's previous pointer
  70. // Then set the new pointer's next pointer equal to p
  71. new_pointer->m_prev = p->m_prev;
  72. new_pointer->m_next = p;
  73.  
  74. // We must now make the previous nodes point to this new node
  75. // Then we must make the next node have its previous point to this node.
  76. new_pointer->m_prev->m_next = new_pointer;
  77. new_pointer->m_next->m_prev = new_pointer;
  78.  
  79. m_size++;
  80. }
  81.  
  82. // I have no idea how to write these function parameters
  83. // Why is there a Set:: before declaring a node pointer?
  84. // Is it because we can only define what a node pointer is after defining it's from the Set class?
  85. Set::Node* Set::findClosestLocation(const ItemType& value) const
  86. {
  87. Node* traversalNode = m_head;
  88.  
  89. while (traversalNode->m_next != nullptr && traversalNode->m_value < value)
  90. {
  91. traversalNode = traversalNode->m_next;
  92. }
  93.  
  94. return traversalNode;
  95. }
  96.  
  97. bool Set::insert(const ItemType& value)
  98. {
  99. Node* closestNode = findClosestLocation(value);
  100.  
  101. // The initialized set actually starts with a node, but with no value inside it and a size of 0
  102. // So we will simply overwrite the value inside it and update its size
  103. if (m_size == 0)
  104. {
  105. m_head->m_value = value;
  106. m_tail = m_head;
  107. m_size++;
  108.  
  109. return true;
  110. }
  111.  
  112. // First check if we already have that value in our linked list
  113. else if (closestNode->m_value == value)
  114. {
  115. return false;
  116. }
  117.  
  118. // Then if it's closest node is the tail, we'll check if it should go before or after the tail
  119. // If we need to create a new tail, then we'll run that function
  120. // If not, we can actually just insert it before the tail (and run it like everything else)
  121. else if (closestNode == m_tail)
  122. {
  123. if (value > m_tail->m_value)
  124. {
  125. insertNewTail(value);
  126. return true;
  127. }
  128. }
  129.  
  130. // insertBefore command actually checks if it should be a new head
  131. insertBefore(closestNode, value);
  132. return true;
  133. }
  134.  
  135. void Set::actualErase(Node* p)
  136. {
  137. // There are 4 cases to consider
  138. // If there is only a single node, then we will delete it and set its next and prev to nullptr
  139. // This is different from deleting a node in the middle because we don't have to link surrounding nodes together after deletion
  140. if (m_size == 1)
  141. {
  142. p->m_next = p->m_prev = nullptr;
  143. }
  144.  
  145. // If we are deleting the head, then we must allocate a new head
  146. else if (p == m_head)
  147. {
  148. m_head = m_head->m_next;
  149. p->m_next->m_prev = nullptr;
  150. }
  151.  
  152. // If we are deleting the tail, then we must allocate a new tail
  153. else if (p == m_tail)
  154. {
  155. m_tail = m_tail->m_prev;
  156. p->m_prev->m_next = nullptr;
  157. }
  158.  
  159. // Because we are deleting a node in the middle, we need to make sure that we are linking the surrounding nodes together
  160. else
  161. {
  162. p->m_prev->m_next = p->m_next;
  163. p->m_next->m_prev = p->m_prev;
  164. }
  165.  
  166. delete p;
  167. m_size--;
  168. }
  169.  
  170. bool Set::erase(const ItemType& value)
  171. {
  172. Node* closestNode = findClosestLocation(value);
  173.  
  174. if (closestNode->m_value != value)
  175. return false;
  176.  
  177. actualErase(closestNode);
  178. return true;
  179. }
  180.  
  181. Set::~Set()
  182. {
  183. while (m_size > 0)
  184. actualErase(m_head);
  185. }
  186.  
  187. void Set::printLinkedList()
  188. {
  189. Node* traversalNode = m_head;
  190.  
  191. while (traversalNode != nullptr)
  192. {
  193. std::cout << traversalNode->m_value << " ";
  194. traversalNode = traversalNode->m_next;
  195. }
  196.  
  197. std::cout << "\n";
  198. }
  199.  
  200. bool Set::contains(const ItemType& value) const
  201. {
  202. Node* closestNode = findClosestLocation(value);
  203. return (closestNode->m_value == value);
  204. }
  205.  
  206. bool Set::get(int i, ItemType& value) const
  207. {
  208. if (i < 0 || i >= m_size)
  209. return false;
  210.  
  211. Node* traversalNode;
  212.  
  213. // Closer to head
  214. if (i < (m_size / 2))
  215. {
  216. traversalNode = m_head;
  217. for (int j = 0; j != i; j++)
  218. {
  219. traversalNode = traversalNode->m_next;
  220. }
  221. }
  222.  
  223. // Closer to tail
  224. else
  225. {
  226. traversalNode = m_tail;
  227. for (int j = m_size - 1; j != i; j--)
  228. traversalNode = traversalNode->m_prev;
  229. }
  230.  
  231. value = traversalNode->m_value;
  232. return true;
  233. }
  234.  
  235. void Set::swap(Set& other)
  236. {
  237. Node* tempHead = other.m_head;
  238. other.m_head = m_head;
  239. m_head = tempHead;
  240.  
  241. int tempSize = other.m_size;
  242. other.m_size = m_size;
  243. m_size = tempSize;
  244.  
  245. // Ask Carey later about swapping tails
  246. // It makes sense that you can't really swap the tails after swapping the heads
  247. // Is there a more elegant solution though?
  248.  
  249. Node* originalTraversalNode = m_head;
  250. for (int i = 1; i < m_size; i++)
  251. originalTraversalNode = originalTraversalNode->m_next;
  252. m_tail = originalTraversalNode;
  253.  
  254. Node* swappedTraversalNode = other.m_head;
  255. for (int i = 1; i < other.m_size; i++)
  256. swappedTraversalNode = swappedTraversalNode->m_next;
  257. other.m_tail = swappedTraversalNode;
  258. }
  259.  
  260. Set::Set(const Set& other)
  261. {
  262. initializeEmptySet();
  263.  
  264. // Copy all non-dummy other Nodes. (This will set m_size.)
  265. // Inserting each new node before the dummy node that m_head points to
  266. // puts the new node at the end of the list.
  267.  
  268. Node* traversalNode = other.m_head;
  269.  
  270. for (int i = 0; i < other.m_size; i++)
  271. {
  272. insert(traversalNode->m_value);
  273. traversalNode = traversalNode->m_next;
  274. }
  275. }
  276.  
  277. Set& Set::operator=(const Set& other)
  278. {
  279. // Trying to avoid aliasing, which is when we use two different pointers/ references to acess the same variable
  280. // This is a problem if we try to first delete the data of the original before reassigning it to itself
  281. // Our solution it see if the parameter has the same object address as the target object
  282. if (this == &other)
  283. {
  284. return *this;
  285. }
  286.  
  287. // Typically, we could simply delete the elements inside the array now, but Carey doesn't want us to use a for loop
  288. // Thus, we will do the Smallberg method of using a copy constructor and a swap method
  289.  
  290. // Call the copy constructor to recreate the other set and then swap elements between the original set and the copied set
  291. // Temporary array will actually deconstruct itself
  292. Set temp(other);
  293. swap(temp);
  294.  
  295. Node* traversalNode = m_head;
  296. int i = 1;
  297.  
  298. std::cout << "\n";
  299.  
  300. // Return the dereferenced contents of the Sets
  301. // Now works for a = b = c as well
  302. return *this;
  303. }
  304.  
  305. void unite(const Set& s1, const Set& s2, Set& result)
  306. {
  307. // We have to worry about aliasing, which is when we use two different pointers/ references to acess the same variable
  308. // The big worry here is if inputted result is actually s1 or s2
  309.  
  310. // If s1, s2, and result are all the same-- then the result is already the union
  311. // If the result is s1, insert s2's elements into the result
  312. // If the result is s2, insert s1's elements into the result
  313. // If the result is its own distinct set, we will first assign it s1's contents, then check if s2 is the same as s1, and if they are different then we will insert s2 into s1
  314.  
  315. // Naturally we will try to insert S2 into S1
  316. const Set* insertedNode = &s2;
  317.  
  318. // First let's check if s1, s2, and result are all the same
  319. if (&result == &s1 && &result == &s2)
  320. {
  321. return;
  322. }
  323.  
  324. // If result is the same as S2, we will make the insertedNode into s1
  325. else if (&result == &s2)
  326. {
  327. insertedNode = &s1;
  328. }
  329.  
  330. // Now that we have determined that result is not s2 and that s1, s2, and result are not all the same
  331. // We will assign result to be equal to s1
  332. // Then we will check if s1 and s2 are the same
  333. // If they are, then we will simply return the set
  334. else
  335. {
  336. result = s1;
  337. if (&s1 == &s2)
  338. {
  339. return;
  340. }
  341. }
  342.  
  343. // If s1 and s2 are not the same, then we will insert one into the other
  344. // If result was the same address as s2, then we will
  345.  
  346. int size_of_insertedNode = insertedNode->size();
  347. ItemType x;
  348.  
  349. for (int i = 0; i < size_of_insertedNode; i++)
  350. {
  351. insertedNode->get(i, x);
  352. result.insert(x);
  353. }
  354. }
  355.  
  356. void butNot(const Set& s1, const Set& s2, Set& result)
  357. {
  358. // We run into aliasing issues if s2 is the same as result
  359. // There will be an issue if we try removing s2 from result when s2 is the same as result
  360. // Because then result w
  361.  
  362. // So we will actually just make a local copy of s2
  363. Set s2LocalCopy(s2);
  364. int size_of_local_copy = s2LocalCopy.size();
  365.  
  366. // Then we will use the assignment operator to make the result equal to s1
  367. // We won't run into aliasing issues because our assignment operator already takes care of that issue
  368. result = s1;
  369.  
  370. for (int i = 0; i < size_of_local_copy; i++)
  371. {
  372. ItemType x;
  373. s2LocalCopy.get(i, x);
  374. result.erase(x);
  375. }
  376.  
  377. }
  378.  
  379. void Set::printDetailedLinkedList()
  380. {
  381. Node* traversalNode = m_head;
  382.  
  383. std::cout << "Size is: " << m_size << "\n";
  384. std::cout << "Head is: " << m_head->m_value << "\n";
  385. std::cout << "Tail is: " << m_tail->m_value << "\n";
  386.  
  387.  
  388. for (int i = 0; i < m_size; i++)
  389. {
  390. std::cout << "The " << i << " Node is: " << traversalNode->m_value << "\n";
  391. std::cout << "Its previous location is: " << &(traversalNode->m_prev) << "\n";
  392. if (traversalNode->m_prev == nullptr)
  393. std::cout << "The previous location is a null pointer. \n";
  394. std::cout << "Its next location is: " << &(traversalNode->m_next) << "\n";
  395. if (traversalNode->m_next == nullptr)
  396. std::cout << "The next location is a null pointer. \n";
  397.  
  398. traversalNode = traversalNode->m_next;
  399. }
  400.  
  401. std::cout << "\n";
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement