SOURCE FILE: ListA.cpp



/*

Implementation file ListA.cpp for the ADT list.
(Patterned after ListA.cpp of p. 148 of Carrano)
Array-based implementation.
listItemType is inventoryItemType

*/

#include <fstream> 
#include <iostream>
#include <iomanip>
#include <assert.h>
#include <string>
#include "ListA.h" //header file

/*

The constructor below uses the "initialize form" for a "data
member" to assign a value of 0 to the list size at the time that
the list is created. (cf pp. 137 of Carrano

*/

List::List() : size(0)
{
} // end default constructor

bool List::isEmpty() const
{
   return bool(size == 0);
} // end isEmpty

int List::getLength() const
{
   return size;
} // end getLength

void List::insert(int index, ListItemType newItem,
		  bool&	success)
{
   success = bool( (index >= 1)	&&
		   (index <= size+1) &&
		   (size < MAX_LIST) );
   if (success)
   {  // make room for new item	by shifting all	items at
      // positions >= index toward the end of the
      // list (no shift	if index == size+1)
      for (int pos = size; pos >= index; --pos)
	 items[translate(pos+1)] = items[translate(pos)];

      // insert	new item
      items[translate(index)] =	newItem;
      ++size; // increase the size of the list by one
   } //	end if
} // end insert

void List::remove(int index, bool& success)
{
   success = bool( (index >= 1)	&& (index <= size) );

   if (success)
   {  // delete	item by	shifting all items at positions	>
      // index toward the beginning of the list
      // (no shift if index == size)
      for (int fromPosition = index+1;
	       fromPosition <= size; ++fromPosition)
	 items[translate(fromPosition-1)] =
			     items[translate(fromPosition)];
      --size; // decrease the size of the list by one
   } //	end if
} // end remove

void List::retrieve(int	index, ListItemType& dataItem,
		    bool& success) const
{
   success = bool( (index >= 1)	&&
		   (index <= size) );

   if (success)
      dataItem = items[translate(index)];
} // end retrieve

int List::translate(int	index) const
{
   return index-1;
} // end translate