[Home]
[Contents]
[Chapter]
[Previous Algorithm]
[Next Algorithm]


Coalesced hashing: insertion


void insert( key, r ) typekey key; dataarray r; { extern int n, nextfree; int i; i = hashfunction( key ); if ( empty(r[i]) ) { r[i].k = key; r[i].next = (-1); n++; } else { /*** Find end of chain ***/ while ( r[i].next!=(-1) && r[i].k!=key ) i = r[i].next; if ( r[i].k==key ) Error /*** key already in table ***/; else { /*** Find next free location ***/ while ( !empty(r[nextfree]) && nextfree>=0 ) nextfree--; if ( nextfree<0 ) Error /*** Table is full ***/; else { r[i].next = nextfree; r[nextfree].k = key; r[nextfree].next = (-1); n++; } } } }

C source (3312.ins.c)



© Addison-Wesley Publishing Co. Inc.