Download presentation
Presentation is loading. Please wait.
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:
5
Why Use Hash Tables? Fast and secure data storage Fast data retrieval
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:
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, Print.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.