Prerequisites: Familiarity with structs and pointers. Familiarity with the dictionary data type.
a. Create a new directory for this lab. I'd suggest
Labs/HashTables, but you can choose whatever you'd like.
b. Copy the code from Examples/HashTables/KR.
c. Review the code to ensure that you understand what's happening.
hash-utest.c has only one unit test.
Add a variety of other tests for
table_get. In particular, you should add tests that
NULLfor keys that are not there.
You might also find it useful to put a bunch of pairs in the dictiomnary. One strategy is to take a series of strings (in an array) and use the first letter as the name (key) and the full string as the value (defn).
a. Extend the code for
install so that it prints out a
message as to where in the hash table it is putting a string. E.g.,
fprintf (stderr, "%s goes in cell %d\n", name, hash (name));
b. Find two keys (names) that give the same index. How many words do you have to enter before you get two keys that have the same index? What does that say about the performance of hash tables?
c. Add a unit test to verify that the hash table performs correctly if we use both of those keys. In particular, you should verify that if both keys have different values, they retrieve the different values.
As you can no doubt tell, I've made some changes to the K&R code.
One obvious change was to separate the implmentation (which relies on
struct nlist) from the interface (which
provides only two simple functions).
Find at least three other changes I've made to the code and consider why I might have made those changes.
Be prepared to discuss these changes in class!
Write a procedure,
dump (hashtable *table), that prints
all of the key/value pairs in the table.
Note: You can't easily unit test this procedure, so you may just want to add a dump at the end of your unit tests.
Traditionally, we use a hash table to look up values by key. But we could
try the reverse, too. Write a function,
char *find_key (hashtable *table, char *value), that returns one key that maps to
If no such key exists, return null.
Sketch an algorithm for deleting a key from the hash table.
Monday, 22 November 2010 [Samuel A. Rebelsky]
Monday, 2 May 2011 [Samuel A. Rebelsky]
for those with extra timesection.
I usually create these pages
on the fly, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Mon May 2 10:34:05 2011.
The source to the document was last modified on Mon May 2 10:33:57 2011.
This document may be found at
A PDF version of this document may be found at