MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / removeBox

Method removeBox

Examples/NoModules/Chapter 12/Ex12_17/Truckload.cpp:79–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

mainFunction · 0.45

Calls 3

getBoxMethod · 0.45
setNextMethod · 0.45
getNextMethod · 0.45

Tested by

no test coverage detected