i am dum
i am dum

Reputation: 3

Hash table with chaining in C

I write code in C for hash table but I want use the hash table with chaining but I don't know how, is there any article or someone can help me how to use hash table with chaining in c?

My code:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char hashTable[26][100];

void printHashTable(){
    int i;
    printf("HASHTABLE\n");
    for(i=0;i<26;i++){
        printf("[%02d] - %s\n", i, hashTable[i]);
    }
}

void menu(){
    printf("\n\nMenu\n");
    printf("1. Input new string\n");
    printf("2. Exit\n");
}

int hash(char s[100]){
    int len = strlen(s); 
    int i;
    int sum = 0;
    for(i=0;i<len;i++){
        sum += s[i];
    }
    printf("sum = %d\n", sum);
    getchar();
    return sum % 26;
}

void insertToHashTable(int index, char s[100]){
    if(strcmp(hashTable[index],"") == 0){
        strcpy(hashTable[index], s);
    }else{
        int i;
        for(i=index+1;i<26;i++){
            if(strcmp(hashTable[i], "")==0){
                strcpy(hashTable[i], s);
                break;
            }   
        }
    }
}

int main(){
    int choice = 0;
    char s[100];
    int index;
    do{
        system("cls");
        printHashTable();
        menu();
        printf("Input choice = ");
        scanf("%d", &choice); getchar();
        if(choice == 1){
            printf("Input string: ");
            scanf("%[^\n]", s); getchar();
            index = hash(s);
            insertToHashTable(index, s);
        }
    }while(choice!=2);
    return 0;
}

Upvotes: 0

Views: 1580

Answers (1)

selbie
selbie

Reputation: 104474

But to make your structure more dynamic, let's update your hash table declaration to be this:

struct hash_table_node
{
   char* value;
   hash_table_node* next;
};

hash_table_node* hashTable[26];

So each hash_table_node is a pointer to a string and the "next" node in the chain.

Then your insert function becomes simple, but it does need to allocate memory for both the node and the string to be copied into it.

void insertToHashTable(int index, const char* s) {

    hash_table_node* node = malloc(sizeof(hash_table_node)); 
 
    node->value = (char*)malloc(strlen(s) + 1);
    strcpy(node->value, s);

    node->next = hashTable[index]; // insert the new string to the front of the linked list
    hashTable[index] = node;
}

Then your printHashTable function can be updated as follows:

void printHashTable(){
    int i;
    printf("HASHTABLE\n");
    for(i=0;i<26;i++){
        printf("Nodes in slot: %d:\n", i);
        hash_table_node* node = hashTable[i];
        while (node)
        {
           printf("%s\n", node->value);
           node = node->next;
        }
    }
}

Not shown, but left as exercise for you:

  • You likely need to update hash to take a const char* s as a parameter instead of a s[100]. But you don't need to change the code within this funciton.

  • The insert function doesn't check to see if the string being added is already in the table.

  • removing elements out of the table and using the appropriate calls to free the allocated memory from the insert function

  • removing all the elements at once to reset the hash table.

Upvotes: 1

Related Questions