-2

I am creating a HashTable in C that uses a Node double pointer and using separate chaining to resolve collisions, but when I run my code it does not place collisions into the linked list.

HTable *createTable(size_t size, int (*hashFunction)(size_t tableSize, 
int key),void (*destroyData)(void *data),void (*printData)(void 
*toBePrinted)){
HTable * h = malloc(sizeof(HTable));

h->size = size;
h->destroyData = destroyData;
h->hashFunction = hashFunction;
h->printData = printData;

h->table = malloc(h->size * sizeof(Node*));
for(int i = 0; i < h->size; i++){

    h->table[i] = malloc(sizeof(Node));
    h->table[i]->key = 0;
    h->table[i]->data = NULL;
    h->table[i]->next = NULL;
}

return h;
}

Node *createNode(int key, void *data){
Node * n = malloc(sizeof(Node));

n->key = key;
n->data = data;
n->next = NULL;

return n;
}

void insertData(HTable *hashTable, int key, void *data){
if(hashTable != NULL){
    Node * n = createNode(key, data);
    int index = hashTable->hashFunction(hashTable->size, key);

    if(hashTable->table[index] != NULL)
        if(hashTable->table[index]->key == key){
            if(hashTable->table[index]->next != NULL){
                n->next = hashTable->table[index]->next;
                hashTable->table[index] = n;
            }
            else
                hashTable->table[index] = n;
        }
        else{
            if(hashTable->table[index]->next != NULL){
                Node * itr = hashTable->table[index];
                while(itr->next != NULL){
                    itr = itr->next;
                }
                itr->next = n;
            }
            else
                hashTable->table[index] = n;
        }
    else{
        hashTable->table[index] = n;
    }
}
}

and the HTable struck and Node struck look like this:

typedef struct Node
{
int key;
void *data;
struct Node *next; 
} Node;

typedef struct HTable
{
size_t size; 
Node **table;
void (*destroyData)(void *data); 
int (*hashFunction)(size_t tableSize, int key); 
void (*printData)(void *toBePrinted); 
}HTable;

I am thinking I am running into a problem in my insertData function when I use the iterator to find the last item in the linked list. That or I am misunderstanding the proper use of a double pointer to a node.

1 Answers1

0

I figured out why the data was not chaining. If the keys did not match it would go to the else statement and the first if statement in that block was asking if hashTable->table[index]->next was not NULL when it should have been asking if hashTable->table[index] was NULL. This is because there could have been one node and its next would be pointing to NULL and the data would then have been overridden. Thanks for the responses and i have added comments which was great advice because it helped me find the error. so thank you @wildplasser

Here is the code if anyone is wondering:

void insertData(HTable *hashTable, int key, void *data){
if(hashTable != NULL){
    Node * n = createNode(key, data);
    int index = hashTable->hashFunction(hashTable->size, key);

    //Check if hashTable table node contains data
    if(hashTable->table[index]->data != NULL)
        //Check if the key at that index matches the incoming key
        if(hashTable->table[index]->key == key){
            //checks if there is more than one node in the chain and replaces the
            //first piece of data in the chain
            if(hashTable->table[index] != NULL){
                n->next = hashTable->table[index]->next;
                hashTable->table[index] = n;
            }
        }
        //if the keys do not match
        else{
            //check if their is one node in the chain
            if(hashTable->table[index] != NULL){
                Node * itr = hashTable->table[index];
                //iterate through the chain to last node
                while(itr->next != NULL){
                    itr = itr->next;
                }
                //insert node into end of chain
                itr->next = n;
            }
        }
    //if there is no data in the node insert the data
    else
        hashTable->table[index] = n;

}
}