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

Method removeBox

Exercises/NoModules/Chapter 12/Soln12_06/Truckload.cpp:94–120  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

92}
93
94bool Truckload::removeBox(SharedBox boxToRemove)
95{
96 Package* previous {nullptr}; // no previous yet
97 Package* current {m_head}; // initialize current to the head of the list
98 while (current)
99 {
100 if (current->m_box == boxToRemove) // We found the Box!
101 {
102 // If there is a previous Package make it point to the next one (Figure 12.10)
103 if (previous) previous->m_next = current->m_next;
104
105 // Update pointers in member variables where required:
106 if (current == m_head) m_head = current->m_next;
107 if (current == m_tail) m_tail = previous;
108
109 current->m_next = nullptr; // Disconnect the current Package from the list
110 delete current; // and delete it
111
112 return true; // Return true: we found and removed the box
113 }
114 // Move both pointers along (mind the order!)
115 previous = current; // - first current becomes the new previous
116 current = current->m_next; // - then move current along to the next Package
117 }
118
119 return false; // Return false: boxToRemove was not found
120}

Callers 1

mainFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected