What am i doing wrong with my linear probing function?
bool HashTable_lp::insert( const string & x )
{
// Insert x as active
int currentPos = findPos( x );
if( isActive( currentPos ) )
return true;
array[ currentPos ] = HashEntry( x, ACTIVE );
if( ++currentSize > array.size( ) / 2 )
rehash( );
return false;
}
You generally want a while loop until you find an empty slot. The other problem is you were equating a cell that your string hashes to being active to meaning that the cell contains the same value you are trying to insert. In general, you need to check the value of your key to insert against existing entries. It may be worth making an insert version that does not do this check for cases where the user can guarantee the item is not already in the hash table to avoid these comparisons…