c - Hash Table with linked list -
c - Hash Table with linked list -
i have problem exercice school.
i have implement hash table using construction :
struct book { char * title; char * auhor; int price; struct book * next; }
so have create function inittable() create hash table table of struct book of 1000 elements. function :
struct book* inittable(){ struct book *tab = malloc(sizeof(struct book) * 1000); homecoming tab; }
note function supposed homecoming first element of table. don't know if syntax correct.
so questions :
is right syntax ?
how can navigate in table ? example, if want go cell 50 of table, how can do?
then, if there's collision in hash table, have create linked list set elements in conflict. each cells of table construction , not pointer of structure, don't understand how can link elements.
sorry asking much , help.
a hash table meant entry in collection key in constant time. in terms of implementation typically consists of "hidden" array of object pointers (not objects themselves). need hashing function converts key array lookup index (integer). example:
valueobjecttype **createhashtable(int hashsize) { valueobjecttype **table = malloc( sizeof(valueobjecttype *) * hashsize); homecoming table; } void hashinsert(valueobjecttype **hashtable, keyobjecttype key, valueobjecttype *value) { int arrayindex = hashingfunction(key); if ( hashtable[arrayindex] != null ) traverselinkedlistandadd(...); else hashtable[arrayindex] = value; } valueobjecttype *lookupbykey(valueobjecttype **hashtable, keyobjecttype key) { int arrayindex = hashingfunction(key); homecoming traverselinkedlistandreturnobjectwithkey(...); }
a hashing function involves taking 1 or more key elements (which can of type) , converting them single integer value , converting array index taking modulo of hash array size.
the purpose linked list in book struct deal hash collisions. hash collision occurs on insert either because given key exists in hash (in case should replace value object reference new object) or because of non-uniqueness of hashing function. when latter happens, linked list used store multiple entries different keys hash same array index (typically iterating through list, replacing element of list if key equality seen, , otherwise tacking new object on @ end).
in illustration code above have separate key object, in order evaluate equality of key needs either included in object (i suspect in case key combination of title , author), or wrapped in meta construction (such "keyvalue" struct contains pointer key , value, hash instead of valueobjecttype). need provide logic evaluate equality/inequality of 2 keys in order handle hash collision case.
i've left hashing function, linked list navigation, , key equality logic unspecified here because instructor wants larn how do.
c table linked-list hashtable
Comments
Post a Comment