// Search for a value by its key char* search(HashTable* hashTable, char* key) { int index = hash(key); Node* current = hashTable->buckets[index]; while (current != NULL) { if (strcmp(current->key, key) == 0) { return current->value; } current = current->next; } return NULL; }
// Delete a key-value pair from the hash table void delete(HashTable* hashTable, char* key) { int index = hash(key); Node* current = hashTable->buckets[index]; if (current == NULL) return; if (strcmp(current->key, key) == 0) { hashTable->buckets[index] = current->next; free(current->key); free(current->value); free(current); } else { Node* previous = current; current = current->next; while (current != NULL) { if (strcmp(current->key, key) == 0) { previous->next = current->next; free(current->key); free(current->value); free(current); return; } previous = current; current = current->next; } } } c program to implement dictionary using hashing algorithms
// Insert a key-value pair into the hash table void insert(HashTable* hashTable, char* key, char* value) { int index = hash(key); Node* node = createNode(key, value); if (hashTable->buckets[index] == NULL) { hashTable->buckets[index] = node; } else { Node* current = hashTable->buckets[index]; while (current->next != NULL) { current = current->next; } current->next = node; } } // Search for a value by its key