Homework 7 - Hash Tables

Due: Tuesday May 31, 2011 at 5:00pm

The purpose of this assignment is to implement an array-based hash table which uses double hashing for collision resolution using procedural programming techniques.

You will be writing a structure called HashTable for this assignment. This will contain an array for the elements and a count of the number of elements stored in the hash table.

You will need the following functions for your hash table:

You should also define the sentinels EMPTY_VALUE and DELETED_VALUE to support the collision detection and resolution handling with deleted values that will be discussed in class. These values CANNOT be inserted into the table, so if someone tries to pass those values to search, insert or remove, return false.

Make sure that insert and remove appropriately adjust the count variable as that is used to see if the hash table is full or empty. On insert, you will use double hashing to find the first array slot that contains either EMPTY_VALUE or DELETED_VALUE, both of which indicate that the slot is available. On remove, find the value to be deleted and replace it with DELETED_VALUE. If the value is not found in the table, remove should print an error message to the screen and return false. When searching for values in the hash table on both remove and search, skip over any slot that contains DELETED_VALUE and do not consider the value "not found" until you reach a slot that contains EMPTY_VALUE.

Your primary hash function (hash1) should be the modulo hash function. Your secondary hash function (hash2) should be the prime-based modulo hash function, which is:

hash2(value) = R - (value % R)
where R is a prime number that is smaller than MAX_CAPACITY. If you wish, you may add a define for this prime number. Note that in order for this to work correctly, MAX_CAPACITY should also be a prime number (such as 1013).

To test your hash table class, write the following menu as your main function:

                 Hash Table Menu
  ======================================================
  1. Insert an integer into the table
  2. Delete an integer from the table
  3. Search the table for an integer
  4. Print the primary and secondary keys for an integer

  0. Exit
  ======================================================
Option 1 should check if the hash table is full. If it is, tell the user that the table is full. Otherwise, prompt the user for the value they wish to insert and then call the insert function in the hash table class to insert that value.

Option 2 should check if the hash table is empty. If it is, tell the user that the table is empty. Otherwise, prompt the user for the value they wish to delete and then call the remove function in the hash table class to remove the value.

Option 3 should also check if the hash table is empty and tell the user that, like Option 2 does. Otherwise, it should prompt the user for a value and call the search function in the hash table class. If search returns true, say the value was found. If search returns false, say the value was not found.

Option 4 should prompt the user for a value. It will then call hash1 and hash2 and print out the integers that they return.

Email me your source code. This is the last required assignment for the quarter. An extra credit assignment will be posted next week.