Member Functions for List ADT - Array Implementation


// *********************************************************
// Unordered List ADT
//
// Array-based implementation.
// *********************************************************

#include "arrlist.h"  // header file

//  default constructor: initialize list to empty

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

// return true if list is empty; false if not

int List::isEmpty() const
{
   return (Size == 0);
}  // end IsEmpty

//  return the number of items in the list

int List::length() const
{
   return Size;
} // end Length

//  add NewItem to the list in NewPosition, if NewPosition is valid
//  move other items down to make room if necessary

int List::insert(int NewPosition,listItemType NewItem)
{
   int Success = ( (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
   return Success;
} // end Insert

//  delete the item in Position from the list, if Position is valid

int List::del(int Position)
{
   int Success = ( (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
   return Success;
}  // end Delete

//  copy the item in Position into DataItem, if Position is valid

int List::retrieve(int Position, listItemType& DataItem) const
{
   int Success = ( (Position >= 1) && (Position <= Size) );

   if (Success)
      DataItem = Items[index(Position)];
      return Success;
 } // end Retrieve

//  convert list position into array subscript

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


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page