0

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

Hogstrom
  • 3,581
  • 2
  • 9
  • 25
proxy
  • 1
  • 1
    Unrelated to your problem, but there are a couple of bad habits in your code. For example the lower-case name `h` for a macro, not using parentheses around macro arguments, using variables in macros not passed as arguments to the macro, casting the result of `malloc` ([you shouldn't do that](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)), using the random number generator without seeding it, no `default` case in your `switch`, general lack of input validation inconsistent indentation, short and non-descriptive names for some variables, etc. – Some programmer dude Mar 20 '22 at 19:10
  • ...also some prefer to use `(N * sizeof *table)` over `(N * sizeof(Node))`. – Weather Vane Mar 20 '22 at 19:16
  • I think you shouldn't have to pass the hashcode to the `insert` function -- it's `insert`'s business to hash the data. Anyway, for strings, you need a hash function that hashes the contents of the string. (Also: a hash table without look-up function?) – M Oehm Mar 20 '22 at 19:28
  • 1
    Aya chafi, `#define N 10 // prime number for size of array` --> note than 10 is not a prime. Why are you using a non-prime? – chux - Reinstate Monica Mar 20 '22 at 19:40
  • i just change it while adding another hashcode function of strings but i end u p keeping the same code with int cuz i got tons of errors – proxy Mar 20 '22 at 21:53
  • Your fundamental table premise is wrong. A collision-list hash table schema should have a table of node pointers; not a table of nodes. it makes a difference, and makes the management near-trivial, *particularly* when having to bucket split the table and relocate existing nodes into new hash chains (something you'll be doing shortly, if not already). – WhozCraig Mar 20 '22 at 22:09

0 Answers0