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

Method removeBox

Exercises/Modules/Chapter 19/Soln19_05/Truckload.cpp:102–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

100}
101
102bool Truckload::removeBox(SharedBox boxToRemove)
103{
104 Package* previous {nullptr}; // no previous yet
105 Package* current {m_head}; // initialize current to the head of the list
106 while (current)
107 {
108 if (current->m_box == boxToRemove) // We found the Box!
109 {
110 // If there is a previous Package make it point to the next one (Figure 12.10)
111 if (previous) previous->m_next = current->m_next;
112
113 // Update pointers in member variables where required:
114 if (current == m_head) m_head = current->m_next;
115 if (current == m_tail) m_tail = previous;
116
117 current->m_next = nullptr; // Disconnect the current Package from the list
118 delete current; // and delete it
119
120 return true; // Return true: we found and removed the box
121 }
122 // Move both pointers along (mind the order!)
123 previous = current; // - first current becomes the new previous
124 current = current->m_next; // - then move current along to the next Package
125 }
126
127 return false; // Return false: boxToRemove was not found
128}
129
130SharedBox& Truckload::operator[](size_t index) const
131{

Callers 1

deliverBoxMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected