// vector.cpp // definition for array-based, unsorted list ADT implementation // based on listClass code in ListA.cpp from Carrano et al. #include "vector.h" Vector::Vector() : Size(0) { } // end default constructor bool Vector::IsEmpty() const { return bool(Size == 0); } // end IsEmpty int Vector::GetLength() const { return Size; } // end GetLength void Vector::InsertItem(int NewPosition, listItemType NewItem, bool &Success) { Success = bool( (NewPosition >= 0) && (NewPosition <= Size) && (Size < 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 == Size) for (int Position = Size - 1; Position >= NewPosition; --Position) { Items[Position + 1] = Items[Position]; } // insert new item Items[NewPosition] = NewItem; ++Size; } // end if } // end InsertItem void Vector::DeleteItem(int Position, bool &Success) { Success = bool( (Position >= 0) && (Position < Size) ); if (Success) { // delete item by shifting all items at positions > // Position toward the beginning of the list // (no shift if Position == Size) for (int FromPosition = Position + 1; FromPosition < Size; ++FromPosition) { Items[FromPosition - 1] = Items[FromPosition]; } --Size; } // end if } // end DeleteItem void Vector::RetrieveItem(int Position, listItemType &DataItem, bool &Success) const { Success = bool( (Position >= 0) && (Position < Size) ); if (Success) DataItem = Items[Position]; } // end RetrieveItem // End of implementation file.