Advertisement
Cinestra

Project 2 before Carey ruins everything

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