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

Method removeBox

Examples/NoModules/Chapter 13/Ex13_10/Truckload.cpp:81–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

mainFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected