// Chap 3, pp 133 - 135 // Change the name of this file to ListA.cpp // ********************************************************* // Implementation file ListA.cpp for the ADT list. // Array-based implementation. // ********************************************************* #include "ListA.h" // header file listClass::listClass() : Size(0) { } // end constructor listClass::listClass(const listClass& L): Size(L.Size) { for (int Position = 1; Position <= L.Size; ++Position) Items[Index(Position)] = L.Items[Index(Position)]; } // end copy constructor listClass::~listClass() { } // end destructor boolean listClass::ListIsEmpty() { return boolean(Size == 0); } // end ListIsEmpty int listClass::ListLength() { return Size; } // end ListLength void listClass::ListInsert(int NewPosition, listItemType NewItem, boolean& Success) { int LastPosition = ListLength(); Success = boolean( (NewPosition >= 1) && (NewPosition <= LastPosition+1) && (LastPosition < MAX_LIST) ); if (Success) { // make room for new item by shifting all items at // positions >= NewPosition toward the end of the // list (no shift if NewPosition == LastPosition+1) for (int Position = LastPosition; Position >= NewPosition; --Position) Items[Index(Position+1)] = Items[Index(Position)]; // insert new item Items[Index(NewPosition)] = NewItem; ++Size; } // end if } // end ListInsert void listClass::ListDelete(int Position, boolean& Success) { int LastPosition = ListLength(); Success = boolean ( (Position >= 1) && (Position <= LastPosition) ); if (Success) { // delete item by shifting all items at positions > // Position toward the beginning of the list // (no shift if Position == LastPosition) for (int FromPosition = Position+1; FromPosition <= LastPosition; ++FromPosition) Items[Index(FromPosition-1)] = Items[Index(FromPosition)]; --Size; } // end if } // end ListDelete void listClass::ListRetrieve(int Position, listItemType& DataItem, boolean& Success) { int LastPosition = ListLength(); Success = boolean ( (Position >= 1) && (Position <= LastPosition) ); if (Success) DataItem = Items[Index(Position)]; } // end ListRetrieve int listClass::Index(int Position) { return Position-1; } // end Index // End of implementation file.