Hashcode of a string in C with Chaining

180 views Asked by At

I have this assignment where I need to implement string hashcoding with chaining in c++ I,ve already tried it but with int data type only could anyone provide some guidance on how to do it? Should I use another hashcode function ??

I’ve already done the display insert function I still have to convert it to string and add three functions of hashcoding and apply it each time to a file and tht each line contain a random word of length from 3 to 30 characters and I have 10000 line which means 10000 words

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

#define N 10         // prime number for size of array
#define h(x) (x % m) // h(x) = x mod m

// Node for List
typedef struct Node { 
    int val;
    struct Node *next;
} Node;

//function's declaration
Node *fill_table(Node *table, int table_range, int number);
Node *insert(Node *table, int hash_index, int val);
void print_table(Node *table, int table_range);

int main() {
    Node *table; // hashtable
    int n, i, j, choice, search;
    int hash_num, val;      

    // allocate table
    table = (Node*) malloc(N * sizeof(Node));

    // make table "heads" NULL
    for (i = 0; i < N; i++) {
        table[i].next = NULL;
    }
    
    printf("--h(x) = xmod%d--\n", N);
    printf("\n\n");  

    while (1) {
        printf("1.Insert random numbers\n");
        printf("2.Show Hash Table\n");
        printf("0.Exit Programm\n");        
        printf("\n--------\n");
        printf("Choice: ");
        
        scanf("%d",&choice);
    
        switch (choice) {
            case 0: break;

            case 1:
                // insert random numbers
                printf("Lot to insert: ");
                scanf("%d", &n);
                table = fill_table(table, N, n);
                printf("Filled HashTable with %d random values\n", n);
                printf("\n--------\n");
                break;
        
            case 2:
                // print hashtable
                printf("-HASHTABLE-\n\n");
                print_table(table, N);
                printf("\n--------\n"); 
                break;  
        }
    }

    return 0;
}

// FUNCTIONS
Node *fill_table(Node *table, int table_range, int number) {
    int i;
    int num;

    for (i = 0; i < number; i++) {
        num = rand() % 10000; // random number from 0 to 9999
        table = insert(table, num % table_range, num);
    }

    return table;
}

void print_table(Node *table, int table_range) {
    int i;
    Node* cur;

    for (i = 0; i < table_range; i++) { // for each list
        if (table[i].next == NULL) { // if empty
            printf("Table[%d] is empty!\n", i);
            continue;
        }
    
        cur = table[i].next;
        printf("Table[%d]", i);
    
        while (cur != NULL) { // else print all the values
            printf("->%d", cur->val);
            cur = cur->next; //cur=cur+1;
        }

        printf("\n");   
    }
}

Node *insert(Node *table, int hash_index, int val) { 
    Node *nn, *cur;

    nn = (Node*)malloc(sizeof(Node));
    nn->val = val;
    nn->next = NULL;
    
    if (table[hash_index].next == NULL) {
        table[hash_index].next = nn;
        return table;
    }
    
    cur = table[hash_index].next;

    while (cur->next != NULL) {
        cur = cur->next; //cur=cur+1;
    }

    cur->next = nn;
    return table;
}
 

hash

0

There are 0 answers