130 likes | 310 Views
Hash Tables in C. Louis Manco. Contents. Explanation of a Hash Table Uses for Hash Tables Application/Implementation In C. What Is a Hash Table?. Data structure Relates values (data) to a dynamic set of strings (keys) “Maps” values
E N D
Hash Tables in C Louis Manco
Contents • Explanation of a Hash Table • Uses for Hash Tables • Application/Implementation In C
What Is a Hash Table? • Data structure • Relates values (data) to a dynamic set of strings (keys) • “Maps” values • Table consists of an array whose elements point to linked lists of information
Example of a Hash Table Image: http://upload.wikimedia.org/wikipedia/commons/7/7d/Hash_table_3_1_1_0_1_0_0_SP.svg
Why Use Hash Tables? • Fast and secure data storage • Fast data retrieval • O(1) • Contain large amounts of data through one small piece of data • Array index
Uses for Hash Tables • Web browser history • Phone/address book • Compiler usage • Manage variable information
Application/Implementation in C Part I • Uses buckets and linked list chaining • Handles collision using linked lists • Called the “separate chaining” method • Buckets contain pointers to elements Image: http://math.hws.edu/eck/cs124/javanotes4/c12/fig2.jpeg
Application/Implementation in C Part II • Element type for buckets • Recall element type for a list typedef struct Nameval Nameval; struct Nameval { char *name; int value; Nameval *next; }; Nameval *symtab[NHASH];
Application/Implementation in C Part III • Lookup/insert algorithm • Takes pointer to the first name char, creation flag integer, and related value as arguments • Create flag allows for creation if specified value does not exist • Returns a pointer to the bucket (array cell)
Hash Function in C • Attempts to uniformly distribute data throughout array • Common algorithm decides hash value (array index) by adding each byte of the string to a multiple of the hash so far • Bits are spread from the new byte through the value so far, mixing the input bytes to get a hash number that has not likely been used yet
Conclusion/Highlights • Hash tables allow for relationships between data values and dynamic sets of strings which are called keys • They allow for fast and secure storage and retrieval • O(1) retrieval (hopefully) • In C, the “separate chain” method is used to handle multiple values landing in one bucket
Source • Kernighan, Brian W., and Rob Pike. The Practice of Programming (Addison-Wesley Professional Computing Series). New York: Addison-Wesley Professional, 1999. Print.