// ********************************************************* // Excerpts from the implementation file ListT.cpp. // ********************************************************* #include "ListT.h" // header file #include // for NULL template struct listNode { T Item; listNode* Next; listNode(); //constructor }; // end struct template listNode::listNode(): Next(NULL) { } // end default constructor template listClass::listClass(): Size(0), Head(NULL) { } // end default constructor template void listClass::ListInsert(int NewPosition, T NewItem, bool& Success) { int NewLength = ListLength() + 1; Success = bool( (NewPosition >= 1) && (NewPosition <= NewLength) ); if (Success) { Size = NewLength; // create new node and place NewItem in it listNode* NewPtr = new listNode; Success = bool(NewPtr != NULL); if (Success) { NewPtr->Item = NewItem; // attach new node to list if (NewPosition == 1) { // insert new node at beginning of list NewPtr->Next = Head; Head = NewPtr; } else { listNode* Prev = PtrTo(NewPosition-1); // insert new node after node // to which Prev points NewPtr->Next = Prev->Next; Prev->Next = NewPtr; } // end if } // end if } // end if } // end ListInsert template listNode* listClass::PtrTo(int Position) { if ( (Position < 1) || (Position > ListLength()) ) return NULL; else // count from the beginning of the list { listNode* Trav = Head; for (int Skip = 1; Skip < Position; ++Skip) Trav = Trav->Next; return Trav; } } // end PtrTo