4.3 Remove

The removal of an entry from a hash table is somewhat easy. The code is captured in listing 4.


Listing 4:hashremoval
 
1void Hash_remove(struct HashTable *_pht, const char *key) 
2{ 
3  int hashvalue = h(key, MAX_ENTRIES); 
4  struct _HashTable *pht = (struct _HashTable *)_pht; 
5 
6  if (pht->table[hashvalue].flags & HASHENTRY_INUSE) 
7  { 
8    --pht->size; 
9    pht->table[hashvalue].flags &= ~HASHENTRY_INUSE; 
10  } 
11}

Well, this code is not entirely correct, but it does capture some of the important concepts. First, we need to check if the key does correspond to any item in the hash table. We check flags and see if the bit corresponding to HASNENTRY_INUSE is set. If so, we proceed to delete the entry. The deletion simply decrements the size and reset the “in use” bit.