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