tablea.cpp

来自「Data Abstraction & Problem Solving with 」· C++ 代码 · 共 75 行

CPP
75
字号
// *********************************************************// Excerpts from the implementation file TableA.cpp.// Sorted array-based implementation.// *********************************************************// The function position must be defined#include "TableA.h" // header filevoid Table::tableInsert(const TableItemType& newItem)            throw(TableException)// Note: Insertion is unsuccessful if the table is full, // that is, if the table already contains MAX_TABLE items.// Calls: position.{   if (size == MAX_TABLE)      throw TableException("TableException: Table full");   // there is room to insert;   // locate the position where newItem belongs   int spot = position(newItem.getKey());   // 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 tableInsertvoid Table::tableDelete(KeyType searchKey)            throw(TableException)// Calls: position.{   // locate the position where searchKey exists/belongs   int spot = position(searchKey);   // is searchKey present in the table?   if ((spot > size) || (items[spot].getKey() != searchKey))      // searchKey not in table      throw TableException("TableException: Item not found on delete");   else   {  // searchKey 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 tableDeletevoid Table::tableRetrieve(KeyType searchKey,                          TableItemType& tableItem) const                          throw(TableException)// Calls: position.{   // locate the position where searchKey exists/belongs   int spot = position(searchKey);   // is searchKey present in table?   if ((spot > size) || (items[spot].getKey() != searchKey))      // searchKey not in table      throw TableException("TableException: Item not found on retrieve");   else      tableItem = items[spot];  // item present; retrieve it}  // end tableRetrievevoid Table::traverseTable(FunctionType visit){   for (int index = 0; index < size; ++index)       visit(items[index]);}  // end traverseTable// End of implementation file.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?