I am writting a look up function for a hashtable. Please veify my function is re
ID: 3820920 • Letter: I
Question
I am writting a look up function for a hashtable. Please veify my function is returning the char* value associated with the key or NULL.
Goal:
/*** Lookup the value associated with a key ***/
// Inputs
// hashtable *h: the hashtable performing the lookup
// char *key: the key string
// Output
// The char * value associated with the key or NULL
char* hashtableLookup(HashTable *h, char *key) {
if(key[0]==0)
return 0;
unsigned long int hashed = hash(key);
// Determine the bucket for the insertion
HashNode *node = h->buckets[hashed];
while (node != NULL) {
if(strcmp(key,node->key) == 0)
return key;
node = node->next;
}
return 0;
}
Explanation / Answer
yes, it is returning key value when if the condition becomes true otherwise it should return NULL, not 0.
char* hashtableLookup(HashTable *h, char *key) {
if(key[0]==0)
return 0;
unsigned long int hashed = hash(key);
// Determine the bucket for the insertion
HashNode *node = h->buckets[hashed];
while (node != NULL) {
if(strcmp(key,node->key) == 0)
return key;
node = node->next;
}
return NULL;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.