02. december 2002 - 16:45Der er
9 kommentarer og 1 løsning
Hashtable
Hej,
Jeg har fundet noget kode som jeg kan bruge til en database, men jeg ved ikke rigtig hvordan jeg skal bruge det i min main. Er lidt forvirret må jeg indrømme. Hvis du kan oplyse mig lidt ville jeg være glad..
/* * Hash function * * Parameters: * * key Some hash table item's key * * Return values: * * >= 0 The item's hash value * * <= -1 Error, the hash value cannot be calculated */ typedef int LLHFUNC(int key);
/* * llhinit: Initialize the hash table * * Parameters: * * t Hash table (duh!) */ void llhinit(LLHTABLE t) { int i = LLHSLOTS;
while (i --) { t[i] = NULL; } }
/* * llhlookup: Access an item * * Parameters: * * t Hash table * * f Pointer to hash function * * key Value uniquely identifying the item to be looked for * * Return values: * * non-NULL The item in question was found; the return value is * a pointer to the corresponding record in the hash table. * * NULL The item was not found in the hash table. Either it just * isn't in there, or the hash function returned an error. */
LLHITEM * llhlookup(LLHTABLE t, LLHFUNC *f, int key) { int slot; struct item *tmp;
/* * llhinsert: Insert an item into the hash table * * Parameters: * * t Hash table * * f Pointer to hash function * * d Item to be inserted * * Return values: * * nonzero The item was inserted successfully * * zero The item could not be inserted. Either the function could * not allocate the amount of memory necessary to store it, * or the hash table already contains an item with the same * key, or the hash function returned an error. * * Note: * * If you know for sure that key values are in fact unique identifiers, * that is, that the calling functions will never try to make the hash * table contain two items with the same key at the same time, you can * speed up the function considerably by deleting the first statement. */ int llhinsert(LLHTABLE t, LLHFUNC *f, LLHITEM d) { int slot; struct item *tmp;
/* delete this line to insert items in constant time: */ if (llhlookup(t, f, LLHKEY(d))) return 0;
/* * llhremove: Remove an item from the hash table * * Parameters: * * t Hash table * * f Pointer to hash function * * key Value uniquely identifying the item to be looked for * * Return values: * * nonzero The item was removed successfully. * * zero The item could not be removed. Either it just wasn't * found in the hash table, or the hash function returned * an error. */ int llhremove(LLHTABLE t, LLHFUNC *f, int key) { int slot; struct item **tmp, *kill;
Ja, du har sq ret min ven. Men hvordan ved den at det er den funktion som den skal kalde, når den ikke engang hedder det samme? Jeg mener man kan jo have mange funktioner som modtager og retunere en integer i sit program..
OKAY! Du har fortjent dine point.. Lige en ting her på falderebet. Ved du tilfældigvis hvad det vil sige at dereferencere en pointer?? Der er ingen point at hente denne gang. :-)
Okay TAK! Jeg troede bare at det var noget med to pointere efter hinanden, altså fx **P det har jeg nemlig set flere gange.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.