Professional Documents
Culture Documents
Review: Pointers Void Pointers Function Pointers
Review: Pointers Void Pointers Function Pointers
Review
Pointers
Void pointers
Function pointers
Hash table
Review:Pointers
Review: Stacks
Review: Queues
items are inserted at the rear and removed from the front.
operations: queue(),dequeue()
Review: Expressions
Inx: (A+B)*(C-D)
prex: *+AB-CD
postx: AB+CD-*
Review
Pointers
Void pointers
Function pointers
Hash table
Void pointers
function.
C allows void pointers
Question: What are some scenarios where you want to
Function pointers
Callbacks
pointers.
Example:
void qsort(void arr, int num,int size, int (fp )( void pa,voidpb))
be done.
The function takes two arguments and returns (<0,0,>0)
Callback (cont.)
Callback (cont.)
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
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
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
Review
Pointers
Void pointers
Function pointers
Hash table
14
Hash table
buckets
000
John Smith
Lisa Smith
Sam Doe
Sandra Dee
Ted Baker
001
entries
Lisa Smith
521-8976
John Smith
521-1234
Sandra Dee
521-9655
Ted Baker
418-4165
Sam Doe
521-5030
002
:
151
152
153
154
:
253
254
255
Hash table
location.
Hash functions are used to generate an evenly distributed
hash value.
15
Hash tables
Hash functions:
A hash function maps its input into a nite range: hash
why?
Other uses of hash functions: cryptography, caches
hashing.
16
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
18
19
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.