Hash Tables in C Louis Manco Contents 1
Hash Tables in C Louis Manco
Contents 1. Explanation of a Hash Table 2. Uses for Hash Tables 3. Application/Implementation In C
What Is a Hash Table? Data structure Relates values (data) to a dynamic set of strings (keys) n “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/7 d/Hash_table_3_1_1_0_0_SP. svg
Why Use Hash Tables? Fast and secure data storage Fast data retrieval n O(1) Contain large amounts of data through one small piece of data n Array index
Uses for Hash Tables Web browser history Phone/address book Compiler usage n Manage variable information
Application/Implementation in C Part I Uses buckets and linked list chaining Handles collision using linked lists n Called the “separate chaining” method Buckets contain pointers to elements Image: http: //math. hws. edu/eck/cs 124/javanotes 4/c 12/fig 2. jpeg
Application/Implementation in C Part II Element type for buckets n Recall element type for a list typedef struct Nameval; struct Nameval { char *name; int value; Nameval *next; }; Nameval *symtab[NHASH];
Application/Implementation in C Part III Lookup/insert algorithm n n n 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 n 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.
- Slides: 12