Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote