Array List Implementation File

file name is listA.cpp


// *********************************************************
// Implementation file listA.cpp for the ADT list.
// Array-based implementation.
// *********************************************************
#include "listA.h"  // header file

listClass::listClass() : Size(0)
{
}  // end default constructor

bool listClass::ListIsEmpty() const
{
   return bool(Size == 0);
}  // end ListIsEmpty

int listClass::ListLength() const
{
   return Size;
} // end ListLength

void listClass::ListInsert(int NewPosition,
                           listItemType NewItem,
                           bool& Success)
{
   Success = bool( (NewPosition >= 1) && 
                   (NewPosition <= Size+1) && 
                   (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+1)
      for (int Position = Size; 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, bool& Success)
{
   Success = bool( (Position >= 1) && (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[Index(FromPosition-1)] = 
                                 Items[Index(FromPosition)];
      --Size;
   }  // end if
}  // end ListDelete

void listClass::ListRetrieve(int Position,
                             listItemType& DataItem, 
                             bool& Success) const
{
   Success = bool( (Position >= 1) && 
                   (Position <= Size) );


   if (Success)
      DataItem = Items[Index(Position)];
 } // end ListRetrieve

int listClass::Index(int Position) const
{
   return Position-1;
} // end Index
//  End of implementation file.