CSC161 2010F Imperative Problem Solving

Laboratory: Hash Tables


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.

c. Review the code to ensure that you understand what's happening.


Exercise 1: Overlaps

a. Find two keys that give the same index in a size 20 hash table.

b. 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.

Exercise 2: Dumping the Table

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.

Exercise 3: Finding Keys by Value

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 value. If no such key exists, return null.

For Those With Extra Time

Sketch an algorithm for deleting a key from the hash table.



Monday, 22 November 2010 [Samuel A. Rebelsky]

  • Created.


Disclaimer: 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 Nov 22 09:32:17 2010.
The source to the document was last modified on Mon Nov 22 09:32:14 2010.
This document may be found at
A PDF version of this document may be found at

Samuel A. Rebelsky,

Copyright © 2010 Samuel A. Rebelsky. This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.