Advertisement
Cinestra

Project 2 -- Before I entirely scrap it to just do what is standard

Feb 27th, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include "Set.h"
  2.  
  3. void Set::initializeEmptySet()
  4. {
  5. m_size = 0;
  6.  
  7. m_head = new Node;
  8. m_head->m_next = nullptr;
  9. m_head->m_prev = nullptr;
  10. }
  11.  
  12. Set::Set()
  13. {
  14. initializeEmptySet();
  15. }
  16.  
  17. // Before we can actually insert the node, we need to implement the insert before function
  18.  
  19. void Set::insertNewHead(const ItemType& value)
  20. {
  21. Node* new_head = new Node;
  22. new_head->m_value = value;
  23.  
  24. m_head->m_prev = new_head;
  25. new_head->m_prev = nullptr;
  26. new_head->m_next = m_head;
  27. m_head = new_head;
  28. }
  29.  
  30. void Set::insertBefore(Node* p, const ItemType& value)
  31. {
  32. //Create a new node
  33.  
  34. Node* new_pointer = new Node;
  35. new_pointer->m_value = value;
  36.  
  37. // Insert it before p
  38.  
  39. // Set the new pointer's previous pointer equal to p's previous pointer
  40. // Then set the new pointer's next pointer equal to p
  41. new_pointer->m_prev = p->m_prev;
  42. new_pointer->m_next = p;
  43.  
  44. // We must now make the previous nodes point to this new node
  45. // Then we must make the next node have its previous point to this node.
  46. new_pointer->m_prev->m_next = new_pointer;
  47. new_pointer->m_next->m_prev = new_pointer;
  48.  
  49. // We have now created the properites of the node.
  50. // We have made sure that it's previous and next pointers point accordingly
  51. // And we have made sure to fix the previous nodes next node to point to this node
  52. // Then we have to make sure to fix the next nodes previous node to point to this one.
  53. }
  54.  
  55. void Set::erase(Node* p)
  56. {
  57. p->m_prev->m_next = p->m_next;
  58. p->m_next->m_prev = p->m_prev;
  59. delete p;
  60.  
  61. m_size--;
  62. }
  63.  
  64. // I have no idea how to write these function parameters
  65. // Why is there a Set:: before declaring a node pointer?
  66. // Is it because we can only define what a node pointer is after defining it's from the Set class?
  67. Set::Node* Set::findClosestLocation(const ItemType& value) const
  68. {
  69.  
  70. // Go through each node looking for a match
  71. Node* p = m_head;
  72.  
  73. // This while loop ensures that it stops if m_value is the target or the first value greater than the target
  74. while (p->m_next != nullptr && p->m_value < value)
  75. {
  76. p = p->m_next;
  77. }
  78.  
  79. return p;
  80. }
  81.  
  82. bool Set::insert(const ItemType& value)
  83. {
  84. Node* p = findClosestLocation(value);
  85.  
  86. if ()
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement