Sams Teach Yourself C++ in 21 Days

(singke) #1
190: if (pNode->GetPart()->GetPartNumber() == PartNumber)
191: break;
192: }
193: if (pNode == NULL)
194: return NULL;
195: else
196: return pNode->GetPart();
197: }
198:
199: void PartsList::Iterate(void (Part::*func)()const) const
200: {
201: if (!pHead)
202: return;
203: PartNode* pNode = pHead;
204: do
205: (pNode->GetPart()->*func)();
206: while ((pNode = pNode->GetNext()) != 0);
207: }
208:
209: void PartsList::Insert(Part* pPart)
210: {
211: PartNode * pNode = new PartNode(pPart);
212: PartNode * pCurrent = pHead;
213: PartNode * pNext = 0;
214:
215: int New = pPart->GetPartNumber();
216: int Next = 0;
217: itsCount++;
218:
219: if (!pHead)
220: {
221: pHead = pNode;
222: return;
223: }
224:
225: // if this one is smaller than head
226: // this one is the new head
227: if (pHead->GetPart()->GetPartNumber() > New)
228: {
229: pNode->SetNext(pHead);
230: pHead = pNode;
231: return;
232: }
233:
234: for (;;)
235: {
236: // if there is no next, append this new one
237: if (!pCurrent->GetNext())
238: {
239: pCurrent->SetNext(pNode);

558 Day 16


LISTING16.5 continued
Free download pdf