Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Set.h"
- void Set::initializeEmptySet()
- {
- m_size = 0;
- m_head = new Node;
- m_head->m_next = nullptr;
- m_head->m_prev = nullptr;
- }
- Set::Set()
- {
- initializeEmptySet();
- }
- // Before we can actually insert the node, we need to implement the insert before function
- void Set::insertNewHead(const ItemType& value)
- {
- Node* new_head = new Node;
- new_head->m_value = value;
- m_head->m_prev = new_head;
- new_head->m_prev = nullptr;
- new_head->m_next = m_head;
- m_head = new_head;
- }
- void Set::insertBefore(Node* p, const ItemType& value)
- {
- //Create a new node
- Node* new_pointer = new Node;
- new_pointer->m_value = value;
- // Insert it before p
- // Set the new pointer's previous pointer equal to p's previous pointer
- // Then set the new pointer's next pointer equal to p
- new_pointer->m_prev = p->m_prev;
- new_pointer->m_next = p;
- // We must now make the previous nodes point to this new node
- // Then we must make the next node have its previous point to this node.
- new_pointer->m_prev->m_next = new_pointer;
- new_pointer->m_next->m_prev = new_pointer;
- // We have now created the properites of the node.
- // We have made sure that it's previous and next pointers point accordingly
- // And we have made sure to fix the previous nodes next node to point to this node
- // Then we have to make sure to fix the next nodes previous node to point to this one.
- }
- void Set::erase(Node* p)
- {
- p->m_prev->m_next = p->m_next;
- p->m_next->m_prev = p->m_prev;
- delete p;
- m_size--;
- }
- // I have no idea how to write these function parameters
- // Why is there a Set:: before declaring a node pointer?
- // Is it because we can only define what a node pointer is after defining it's from the Set class?
- Set::Node* Set::findClosestLocation(const ItemType& value) const
- {
- // Go through each node looking for a match
- Node* p = m_head;
- // This while loop ensures that it stops if m_value is the target or the first value greater than the target
- while (p->m_next != nullptr && p->m_value < value)
- {
- p = p->m_next;
- }
- return p;
- }
- bool Set::insert(const ItemType& value)
- {
- Node* p = findClosestLocation(value);
- if ()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement