CS 11 C Track: Lecture 7: Last Week: Structs,, Linked Lists This Week
CS 11 C Track: Lecture 7: Last Week: Structs,, Linked Lists This Week
Hash tables are a new data structure Like an array indexed with strings e.g.
height[Jim] = 6;
/* not C code */
Very fast lookup (O(1) i.e. constant time) Flexible: can add/delete elements easily
Want to associate a string (key) with a value Generate an integer hash value from the string key
array length is large (128 in lab 7) array values start off as NULL pointers (empty lists) no linked list should ever get larger than a few elements
Many ways to do it We choose a particularly simple (and lame) way Treat the string as an array of chars Treat each char as a small integer (0 - 127) C allows this Sum up the values of all the characters Take the sum mod 128 (the array length)
Gives an integer in the range 0-127 that's our index into the array
Look up the value corresponding to a particular key Change the value corresponding to an existing key in the table
compute hash value to get array index find array location if NULL, not there (return "not found" value) if not NULL, search for key in linked list
if found, return node value if not found, not there (return "not found" value)
How to change the value corresponding to a given key (or add a new key/value pair): compute hash value to get array index find array location if NULL, add node with key/value pair if not NULL, search for key in linked list if found, change node value if not found, add new node to list (anywhere in list!)
nodes in linked list not in any order so can add to any place in list most people try to add to the end of the list actually easier to add to beginning of list either way, have to set some pointer values to different values
It's a struct which contains that array Easy to make mistakes with this Think of it as a box containing the array Practice in handling more complex data structures Real hash tables would have more fields e.g. length of array to permit resizing of the array
Lab 7
Pretty routine application of hash tables One likely problem involving a memory leak
Sometimes want to conditionally compile code If some condition met, compile this code else do nothing, or do something else Examples:
Debugging code:
int value = 10;
#define DEBUG
-D option means to Define DEBUG This makes the debugging code compile Otherwise it wont compile Usually best to do it this way
C preprocessor: #else
#ifndef includes code if something is not defined assert is defined using #ifndef e.g. /* expands to: */
% gcc -DNDEBUG foo.c -o foo Then all assertions are removed from code during compilation
#if REVISION == 1 /* revision 1 code */ #elif REVISION == 2 /* revision 2 code */ #else /* generic code */ #endif
Difficult to prevent
/* FOO_H */
extern (1)
Sometimes many files need to share some data e.g. global variable
Can only define in one place Put extern declaration in header file Means: this is defined somewhere else
extern (2)
/* In header file "foo.h": */ extern int max_value;
const
Next week
Most of C language has been covered Virtual machines (!) More integer types: short, long, unsigned Wrapping up