SOURCE FILE: listCls.cpp
// Implementation file listCls.cpp for the ADT list.
// (Patterned after ListA.cpp of p. 137 of Carrano)
// Array-based implementation.
// listItemType is inventoryItemType
// *********************************************************
#include <fstream.h> //includes iostream.h
#include <iomanip.h>
#include <assert.h>
#include <string>
#include "listCls.h" // header file
// This constructor 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. 130-131 of Carrano
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.