1 / 12

Hash Tables in C

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

illias
Download Presentation

Hash Tables in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hash Tables in C Louis Manco

  2. Contents • Explanation of a Hash Table • Uses for Hash Tables • Application/Implementation In C

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

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

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

  6. Uses for Hash Tables • Web browser history • Phone/address book • Compiler usage • Manage variable information

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

  8. 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];

  9. 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)

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

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

  12. Source • Kernighan, Brian W., and Rob Pike. The Practice of Programming (Addison-Wesley Professional Computing Series). New York: Addison-Wesley Professional, 1999. Print.

More Related