Sams Teach Yourself C++ in 21 Days

(singke) #1
Advanced Inheritance 567

16


195: void PartsList::Iterate(void (Part::*func)()const) const
196: {
197: if (!pHead)
198: return;
199: PartNode* pNode = pHead;
200: do
201: (pNode->GetPart()->*func)();
202: while ((pNode = pNode->GetNext()) != 0);
203: }
204:
205: void PartsList::Insert(Part* pPart)
206: {
207: PartNode * pNode = new PartNode(pPart);
208: PartNode * pCurrent = pHead;
209: PartNode * pNext = 0;
210:
211: int New = pPart->GetPartNumber();
212: int Next = 0;
213: itsCount++;
214:
215: if (!pHead)
216: {
217: pHead = pNode;
218: return;
219: }
220:
221: // if this one is smaller than head
222: // this one is the new head
223: if (pHead->GetPart()->GetPartNumber() > New)
224: {
225: pNode->SetNext(pHead);
226: pHead = pNode;
227: return;
228: }
229:
230: for (;;)
231: {
232: // if there is no next, append this new one
233: if (!pCurrent->GetNext())
234: {
235: pCurrent->SetNext(pNode);
236: return;
237: }
238:
239: // if this goes after this one and before the next
240: // then insert it here, otherwise get the next
241: pNext = pCurrent->GetNext();
242: Next = pNext->GetPart()->GetPartNumber();
243: if (Next > New)
244: {
245: pCurrent->SetNext(pNode);
246: pNode->SetNext(pNext);

LISTING16.6 continued

Free download pdf