You are on page 1of 2

const int MAX_TABLE = 11; template < class tK,class tD > class Table{ public: Table(); // Table constructor

void insert(const tK & key, const tD & data); bool lookup(const tK & key, tD & data); void deleteKey(const tK & key); void dump(); // print the contents of the hash private: enum slotType {Empty, Deleted, InUse}; struct slot { slotType slotStatus; tK key; tD data; }; slot T[MAX_TABLE]; int entries; int hash(const tK & key); int probe(const int pos); bool search(int &pos, const tK & target);}; template < class tK,class tD > int Table < tK, tD >::hash(const tK & key) { return key % MAX_TABLE;} template < class tK, class tD > int Table < tK, tD >::probe(const int pos) { if (pos < MAX_TABLE - 1) return pos + 1; else return 0;} template < class tK, class tD > bool Table<tK,tD>::search(int &pos, tK target) { for (;T[pos].slotStatus != Empty; pos = probe(pos)) if (T[pos].slotStatus == InUse && T[pos].key == target) true; return false;}

return

template < class tK, class tD > Table < tK, tD >::Table() { entries = 0; int i; for (i = 0; i < MAX_TABLE; i++) T[i].slotStatus = Empty;} template < class tK, class tD > void Table < tK, tD >::insert(tK key, tD { assert(entries < MAX_TABLE - 1); int pos(hash(key)); if (!search(pos, key)) { pos = hash(key); while (T[pos].slotStatus == InUse) pos = probe(pos); entries++; } T[pos].slotStatus = InUse; T[pos].key = key; T[pos].data = data;} data)

template < class tK, class tD > bool Table < tK, tD >::lookup(tK key, tD & data) { int pos(hash(key)); if (search(pos, key)) { data = T[pos].data; return true; } else return false;} template < class tK, class tD > void Table < tK, tD >::deleteKey(const tK & key) { int pos(hash(key)); if (search(pos, key)) { T[pos].slotStatus = Deleted;

entries--;

}}