Question
Using C, Implement a hash table. A hash table is a data structure that acts like a dictionary. A dictionary has keys and values. For
Using C, Implement a hash table. A hash table is a data structure that acts like a dictionary. A
dictionary has keys and values. For instance, a dictionary of words has the words grouped by their initial
letter in alphabetical order. In this case, the keys are single characters and the values are linked lists that
store strings; one string per word. This data structure can be seen as a generalization of the array. For
instance,
dictionary[a] = {"Alphabet", "airplane", "Access"}
dictionary[c] = {"Car", "Cast", "condition"}
In the example above, the keys are 'a' and 'c', while the values are f "Alphabet", "airplane", "Access"g and
f"Car", "Cast", "condition"g. As an observation, a hash table or dictionary can be seen as a data structure
that maps keys to values. In this particular example, the hash table maps letters to list of strings.
To implement this data structure, declare the following structure:
HashTable This structure will contain the necessary members to construct a hash table to build a dictionary
of words. It must have the following members:
1. keys: A pointer to an array of characters.
2. values: A pointer to an array of linked lists (Hint: Use double pointers.).
The keys member must contain enough entries to store words from the English alphabet. The values member
must contain one linked list for every key. Note that the linked lists will be instances of your implementation
of Part 1. Make sure that the i-th key has a corresponding list in the i-th entry in the array values. Because
this data structure can be accessed by multiple threads in parallel, make sure you use the mutex of the list
to guard resources.
Your implementation must ensure the following functions:
Constructor:
1. Name: CreateHashTable
2. Return: A pointer to the hash table.
Destructor:
1. Name: DestroyHashTable
2. Return: Nothing
3. Input: A double pointer to the hash table.
Put: Inserts a string into the table, only if it is not present.
1. Name: Put
2. Return: TRUE upon successful insertion, FALSE otherwise.
3. Input: A pointer to the string to insert.
4. Input: A pointer to the hash table.
GetList: Returns the list given a key.
1. Name: GetList
2. Return: A pointer to the list for a given key.
3. Input Argument 1: The key character.
4. Input Argument 2: The hash table.
GetMutex: Returns a mutex instance to guard resources.
1. Name: GetMutex
2. Return: A pointer to a mutex instance.
3. Input Argument 1: The key character.
4. Input Argument 2: The hash table.
GetIndex: Returns the index of a key. In other words, this function maps a single character to an index to
access elements in the keys, and values arrays of the hash table.
1. Name: GetIndex
2. Return: An index to access the key or the linked list for a given letter. Return -1 when a corner case
is identied.
3. Input: The key character.
4. Note: There are 26 letters in the English alphabet, which means that a letter must be mapped to an
index in the range [0; 25]. Recall that a character in C can be used as a number.
The hash table must be thread-safe in order to avoid race conditions. For this, use mutexes to guard the
necessary resources. Test your hash table implementation by creating a binary using the test hash table.c.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started