Advertisement
Cinestra

PROJECT 2 THAT WORKS!!!!

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