| 76 | } |
| 77 | |
| 78 | bool Truckload::removeBox(SharedBox boxToRemove) |
| 79 | { |
| 80 | Package* previous {nullptr}; // no previous yet |
| 81 | Package* current {m_head}; // initialize current to the head of the list |
| 82 | while (current) |
| 83 | { |
| 84 | if (current->getBox() == boxToRemove) // We found the Box! |
| 85 | { |
| 86 | // If there is a previous Package make it point to the next one (Figure 12.10) |
| 87 | if (previous) previous->setNext(current->getNext()); |
| 88 | |
| 89 | // Update pointers in member variables where required: |
| 90 | if (current == m_head) m_head = current->getNext(); |
| 91 | if (current == m_tail) m_tail = previous; |
| 92 | if (current == m_current) m_current = current->getNext(); |
| 93 | |
| 94 | current->setNext(nullptr); // Disconnect the current Package from the list |
| 95 | delete current; // and delete it |
| 96 | |
| 97 | return true; // Return true: we found and removed the box |
| 98 | } |
| 99 | // Move both pointers along (mind the order!) |
| 100 | previous = current; // - first current becomes the new previous |
| 101 | current = current->getNext(); // - then move current along to the next Package |
| 102 | } |
| 103 | |
| 104 | return false; // Return false: boxToRemove was not found |
| 105 | } |