Online Book Reader

Home Category

Mastering Algorithms With C - Kyle Loudon [78]

By Root 1521 0

htbl->h1 = h1;

htbl->h2 = h2;

htbl->match = match;

htbl->destroy = destroy;

/*****************************************************************************

* *

* Initialize the number of elements in the table. *

* *

*****************************************************************************/

htbl->size = 0;

return 0;

}

/*****************************************************************************

* *

* ---------------------------- ohtbl_destroy ----------------------------- *

* *

*****************************************************************************/

void ohtbl_destroy(OHTbl *htbl) {

int i;

if (htbl->destroy != NULL) {

/**************************************************************************

* *

* Call a user-defined function to free dynamically allocated data. *

* *

**************************************************************************/

for (i = 0; i < htbl->positions; i++) {

if (htbl->table[i] != NULL && htbl->table[i] != htbl->vacated)

htbl->destroy(htbl->table[i]);

}

}

/*****************************************************************************

* *

* Free the storage allocated for the hash table. *

* *

*****************************************************************************/

free(htbl->table);

/*****************************************************************************

* *

* No operations are allowed now, but clear the structure as a precaution. *

* *

*****************************************************************************/

memset(htbl, 0, sizeof(OHTbl));

return;

}

/*****************************************************************************

* *

* ----------------------------- ohtbl_insert ----------------------------- *

* *

*****************************************************************************/

int ohtbl_insert(OHTbl *htbl, const void *data) {

void *temp;

int position,

i;

/*****************************************************************************

* *

* Do not exceed the number of positions in the table. *

* *

*****************************************************************************/

if (htbl->size == htbl->positions)

return -1;

/*****************************************************************************

* *

* Do nothing if the data is already in the table. *

* *

*****************************************************************************/

temp = (void *)data;

if (ohtbl_lookup(htbl, &temp) == 0)

return 1;

/*****************************************************************************

* *

* Use double hashing to hash the key. *

* *

*****************************************************************************/

for (i = 0; i < htbl->positions; i++) {

position = (htbl->h1(data) + (i * htbl->h2(data))) % htbl->positions;

if (htbl->table[position] == NULL || htbl->table[position] == htbl->

vacated) {

/***********************************************************************

* *

* Insert the data into the table. *

* *

***********************************************************************/

htbl->table[position] = (void *)data;

htbl->size++;

return 0;

}

}

/*****************************************************************************

* *

* Return that the hash functions were selected incorrectly. *

* *

*****************************************************************************/

return -1;

}

/*****************************************************************************

* *

* ----------------------------- ohtbl_remove ----------------------------- *

* *

*****************************************************************************/

int ohtbl_remove(OHTbl *htbl, void **data) {

int position,

i;

/*****************************************************************************

* *

* Use double hashing to hash the key. *

* *

*****************************************************************************/

for (i = 0; i < htbl->positions; i++) {

position = (htbl->h1(*data) + (i * htbl->h2(*data))) %

Return Main Page Previous Page Next Page

®Online Book Reader