// Chap 11, pp 526 - 528
// Rename this file as TableA.cpp.
// *********************************************************
// Excerpts from the implementation file TableA.cpp.
// Sorted array-based implementation.
// *********************************************************
#include "Table.h" // header file
void tableClass::TableInsert(tableItemType NewItem,
boolean& Success)
// Note: Insertion is unsuccessful if the table is full,
// that is, the table already contains MAX_TABLE items.
// Calls: Position.
{
// is there room for insertion?
Success = boolean(Size < MAX_TABLE);
if (Success)
{ // there is room to insert;
// locate the position where NewItem belongs
int Spot = Position(NewItem.Key);
// shift up to make room for the new item
for (int Index = Size-1; Index >= Spot; --Index)
Items[Index+1] = Items[Index];
// make the insertion
Items[Spot] = NewItem;
++Size;
} // end if
} // end TableInsert
void tableClass::TableDelete(keyType SearchKey,
boolean& Success)
// Calls: Position.
{
// locate the position where SearchKey exists/belongs
int Spot = Position(SearchKey);
// is Searchkey present in the table?
Success = boolean((Spot <= Size) &&
(Items[Spot].Key == SearchKey));
if (Success)
{ // Search Key in Table
--Size; // delete the item
// shift down to fill the gap
for (int Index = Spot; Index < Size; ++Index)
Items[Index] = Items[Index+1];
} // end if
} // end TableDelete
void tableClass::TableRetrieve(keyType SearchKey,
tableItemType& TableItem,
boolean& Success)
// Calls: Position.
{
// locate the position where SearchKey exists/belongs
int Spot = Position(SearchKey);
// is Searchkey present in table?
Success = boolean((Spot <= Size) &&
(Items[Spot].Key == SearchKey));
if (Success)
TableItem = Items[Spot]; // item present; retrieve it
} // end TableRetrieve
void tableClass::TraverseTable(functionType Visit)
{
for (int Index = 0; Index < Size; ++Index)
Visit(Items[Index]);
} // end TraverseTable
// End of implementation file.
               (
geocities.com/siliconvalley/program/2864/ds)                   (
geocities.com/siliconvalley/program/2864)                   (
geocities.com/siliconvalley/program)                   (
geocities.com/siliconvalley)