const int hash_size = 997; // a prime number of appropriate size class Hash_table { public : Hash_table(); void clear(); Error_code insert( const Record &new_entry); Error_code retrieve( const Key &target, Record &found) const ; Error_code remove( const Key &target, Record &found); private : List
table[hash_size]; }; Hash_table::Hash_table(){ } void Hash_table::clear() { for ( int i = 0; i < hash_size; i++) table[i].clear(); } Error_code Hash_table::retrieve( const Key &target, Record &found) const // 由关键字找到对应项整个记录 { int position; Error_code outcome; int probe =hash( ); outcome = sequential_search(table[probe],target, position); if (outcome== success) table[ ].retrieve(position,found); return outcome; } Error_codeHash_table::insert( const Record &new_entry) { Record old_copy; if (retrieve(new_entry, old_copy) == success) // 由 new_entry 中的关键字找到对应项整个记录存入 old_copy 中 return duplicate_error; int probe =hash( ); return table[ ].insert(0,new_entry); } Error_codeHash_table::remove( const Key &target,Record &found) { int position; int probe =hash( ); if (sequential_search(table[probe], target, position) != success) return not_present; return table[ ].remove(position,found); }