C_lecture_7 - CS 11 C track: lecture 7 Last week: structs,...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 11 C track: lecture 7 ± Last week: structs, typedef , linked lists ± This week: ± hash tables ± more on the C preprocessor ± extern ± const
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Hash tables (1) ± Data structures we’ve seen so far: ± arrays ± struct s ± linked lists
Background image of page 2
Hash tables (2) ± 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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Hash tables (3) ± Want to associate a string ( key ) with a value ± Generate an integer hash value from the string key ± different keys should generate different hash values ± Use hash value as index into an array of linked lists ± 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
Background image of page 4
Hash tables (4) . . . “Jim” 119 (hash value) 6 height “Jim” key value next (NULL)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Hash tables (5) ± Generating the hash value from the string ± 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
Background image of page 6
Hash tables (6)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 26

C_lecture_7 - CS 11 C track: lecture 7 Last week: structs,...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online