Question: Please write the following in c: In a file hashing.c, do the following: Declare a struct type HASHCELL, which is used to contain data in
Please write the following in c:
In a file hashing.c, do the following:
Declare a struct type HASHCELL, which is used to contain data in a hash table. A HASHCELL should contain two fields: a word field that is a string it should be of type char *, so its a pointer, not an allocated array and a next field that is a pointer to a HASHCELL, so that HASHCELLs can be linked together in a list.
Declare a global variable hashtable, representing a hash table, which is an array of HASHCELL pointers. You should use #define to define a constant SIZE to be 100 and declare hashtable to have SIZE elements.
Define a function hash_string which takes a string (a char *) as a parameter and returns an unsigned integer between 0 and SIZE -1, by hashing the string. You are free to choose your own hash algorithm, but here is a simple one you can use:
o Define an unsigned integer variable hash and initialize it to 1. o In a loop that iterates over each character in the string, set hash equal to (hash * 7) + c in each iteration of the loop, where c is the current character. Since a char is just an 8-bit number, so its fine to do arithmetic on it. o Return the value of hash mod SIZE (where % is the mod operator). Remember that you can recognize the end of a string by the terminating 0. The quality of the hash (i.e., how evenly it distributes hash values between 0 and SIZE - 1) isnt so important here.
Define a function insert_hash_cell that takes a string (again, a char *) as a parameter and inserts the string into the hash table as follows: o It calls hash_string on the string, assigning the result of the hash to an unsigned integer variable index. o It creates a HASHCELL (using malloc), such that the word field of the cell points to the string. IMPORTANT: You will need to make a copy of the string by performing the following steps:
Using malloc again, allocate a block of memory large enough to hold the string (including the 0 at the end). I suggest using the built-in strlen function, described at the bottom of this assignment, which gives you the length of a string without the 0 at the end (so youll need to add 1).
Set the word field of the cell to point to the new block of memory.
Copy the characters of the string into the new block. I suggest using the strcpy function, described at the bottom of this assignment. o It inserts the new cell into the linked list of cells pointed to by hashtable[index]. However, if the word in the new cell already exists in that linked list, do not insert the new cell. This prevents duplicate words from being inserted into the hash table. I suggest using the built-in strcmp function, described below, to compare two strings to see if they are the same.
Define a function print_hash_table() that prints out the elements of the hash table. Specifically, in a loop, for each i from 0 to SIZE-1, the function should print out i, then :, then (on the same line) all the words in the linked list at hashtable[i], and then a carriage return. This way, the list of words at each element of the hash table is printed on their own line
Still within hashing.c, define a main function that does the following:
It declares a variable str that is an array of 100 chars (which should be large enough to hold any string that is read in from the terminal).
In a loop, it uses scanf to repeatedly read a string from the terminal into str, and then calls insert_hash_cell on str to insert the string into the hash table (which is why insert_hash_cell has to make a copy of the string). The loop should stop when there are no more strings to read, which scanf will indicate by returning the value EOF. That is, your loop could look like, while (scanf() != EOF) {}.
It calls print_hash_table()to print the contents of the hashtable.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
