Professional Documents
Culture Documents
Review
Pointers
Void pointers
Function pointers
Hash table
1
Review:Pointers
1
Review: Stacks
2
Review: Queues
3
Review: Expressions
• Infix: (A+B)*(C-D)
• prefix: *+AB-CD
• postfix: AB+CD-*
4
6.087 Lecture 8 – January 21, 2010
Review
Pointers
Void pointers
Function pointers
Hash table
5
Void pointers
5
Function pointers
6
Callbacks
pointers.
Example:
void qsort(void∗ arr, int num,int size, int (∗fp )( void∗ pa,void∗pb))
7
Callback (cont.)
8
Callback (cont.)
Iterating:
s t r u c t node∗ phead ;
/ ∗ p o p u l a t e somewhere ∗ /
void p r i n t ( void ∗ p , void ∗ arg )
{
s t r u c t node∗ np =( s t r u c t node ∗ ) p ;
p r i n t f ( "%d " , np−>data ) ;
}
a p p l y ( phead , p r i n t , NULL ) ;
10
Callback (cont.)
Counting nodes:
11
Array of function pointers
switch ( ps−>t y p e )
{
case SQUARE:
draw_square ( ps ) ; break ;
case RECT:
d r a w _ r e c t ( ps ) ; break ;
...
}
}
12
Array of function pointers
drawfn f p [ 4 ] =
( ∗ f p [ ps−>t y p e ] ) ( ps ) ; / ∗ c a l l t h e c o r r e c t f u n c t i o n ∗ /
}
13
6.087 Lecture 8 – January 21, 2010
Review
Pointers
Void pointers
Function pointers
Hash table
14
Hash table
14
Hash table
15
Hash tables
Hash functions:
• A hash function maps its input into a finite range: hash
value, hash code.
• The hash value should ideally have uniform distribution.
why?
• Other uses of hash functions: cryptography, caches
(computers/internet), bloom filters etc.
• Hash function types:
• Division type
• Multiplication type
• Other ways to avoid collision: linear probing, double
hashing.
16
Hash table: example
s t r u c t wordrec ∗ n e x t ;
};
/ ∗ hash bucket ∗ /
s t r u c t wordrec ∗ t a b l e [ MAX_LEN ] ;
17
Hash table: example
18
Hash table: example
19
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.