The following code is an implementation of an hashtable in C.
hashTable.c:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hashTable.h"
int hashCode(int key) {
return key % SIZE;
}
/*
By given key returns a pointer to a DataItem with the same key
Input: Pointer to hashArray array, int key value
Output: If key found - the pointer to a DataItem else NULL
*/
DataItem *getValueByKey (DataItem** hashArray, int key) {
/* Get the hash */
int hashIndex = hashCode(key);
/* Move in array until an empty */
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
/* Go to next cell */
++hashIndex;
/* Wrap around the table */
hashIndex %= SIZE;
}
return NULL;
}
/*
Adding a DataItem to a hashArray
Input: Pointer to hashArray array, int key value, char* (string) data value
Output: None
*/
void putValueForKey (DataItem** hashArray, int key, char *data) {
/* Get the hash */
int hashIndex = hashCode(key);
DataItem *item = (DataItem*) malloc(sizeof(DataItem));
item->data = data;
item->key = key;
/* Move in array until an empty or deleted cell */
while(hashArray[hashIndex] != NULL) {
/* Go to next cell */
++hashIndex;
/* Wrap around the table */
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
/*
Deleting a DataItem node from an hash array
Input: Pointer to hashArray array, pointer to the item we want to delete
Output: The deleted item pointer
*/
DataItem* deleteHash (DataItem** hashArray, DataItem* item) {
int key;
int hashIndex;
if (item == NULL)
{
return NULL;
}
key = item->key;
/* Get the hash */
hashIndex = hashCode(key);
/* Move in array until an empty */
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key) {
DataItem* temp = hashArray[hashIndex];
/* Assign a dummy item at deleted position */
hashArray[hashIndex] = NULL;
return temp;
}
/* Go to next cell */
++hashIndex;
/* Wrap around the table */
hashIndex %= SIZE;
}
return NULL;
}
/*
displaying an hash array
Input: Pointer to hashArray array
Output: None
*/
void displayHashTable (DataItem** hashArray) {
int i = 0;
for (i = 0; i < SIZE; i++) {
if (hashArray[i] != NULL)
printf(" (%d,%s)",hashArray[i]->key,hashArray[i]->data);
else
printf(" ~~ ");
}
printf("\n");
}
/*
Freeing an hash array
Input: Pointer to hashArray array
Output: None
*/
void freeHashTable (DataItem** hashArray) {
int i = 0;
for (i = 0; i < SIZE; ++i)
{
if (hashArray[i] != NULL) {
free(hashArray[i]);
}
}
}
/*
Initialize an hash array by setting all the nodes to NULL
Input: Pointer to hashArray array
Output: None
*/
void initializeHashArray (DataItem** hashArray) {
int i = 0;
for (i = 0; i < SIZE; i++) {
hashArray[i] = NULL;
}
}
int main() {
DataItem* hashArray[SIZE];
initializeHashArray(hashArray);
putValueForKey(hashArray, 100, "MAIN");
putValueForKey(hashArray, 107, "LOOP");
putValueForKey(hashArray, 121, "END");
putValueForKey(hashArray, 122, "STR");
putValueForKey(hashArray, 129, "LENGTHHHHHHHHHHHHHHHHHHHHH");
putValueForKey(hashArray, 132, "K");
putValueForKey(hashArray, 133, "M1");
displayHashTable(hashArray);
DataItem* item = getValueByKey(hashArray, 100);
if (item != NULL) {
printf("Element found: %s\n", item->data);
}
else {
printf("Element not found\n");
}
deleteHash(hashArray, item);
item = getValueByKey(hashArray, 100);
if (item != NULL) {
printf("Element found: %s\n", item->data);
}
else {
printf("Element not found\n");
}
displayHashTable(hashArray);
freeHashTable(hashArray);
}
I believe this code is well commented and very understandable, if you think it's not please let me know.
The main is there just for testing and will not be part of the final code.
My main concerns are:
The free function (freeHashTable) because I don't know how to test it so I don't know if it's working.
The while loops that might get into an infinite loop condition for some reason.
Also this is the first time I'm trying to implement hash table, so if I'm missing something from the logical aspect of hash code explanation will be very welcomed.
hashTable.h:
#define SIZE 20
struct DataItem {
char *data;
int key;
}typedef DataItem;
DataItem *getValueByKey (DataItem** hashArray, int key);
void putValueForKey (DataItem** hashArray, int key, char *data);
DataItem* deleteHash (DataItem** hashArray, DataItem* item);
void displayHashTable (DataItem** hashArray);
void freeHashTable (DataItem** hashArray);
void initializeHashArray (DataItem** hashArray);
Thanks in advance!