Hash Code (II)

On the other hand, you usually don't have a perfect hash function. Here's one way to deal with this problem:
#define TABLE_SIZE 32768

typedef char *stringSet[TABLE_SIZE];

void initialize(stringSet a);  /* same as before */

int insert(stringSet a, char *s)
{
   unsigned int h, i;

   h = hashf( s );
   if ( a[h] != NULL )
     {
     i = h + 1;
     while( ( a[i] != NULL ) && ( h != i ) )
       i = ( i + 1 ) % TABLE_SIZE;
     if ( i == h ) 
       return -1;
     h = i;
     }
    
   a[h] = s;
   return 0;

}


int isMember( stringSet a, char *s )
{
  unsigned int h, i;
  int t;

  h = hashf( s );
  i = h;

  do
    {
     if ( a[i] == NULL )
       return FALSE;
     t = strcmp( a[i], s );
     i = ( i + 1 ) % TABLE_SIZE;
    }
  while ( ( t != 0 ) && ( h != i ) );

  return ( t = 0 );
} 

Next Slide