source code in c hash bucket

Solutions on MaxInterview for source code in c hash bucket by the best coders in the world

showing results for - "source code in c hash bucket"
Gael
10 Jun 2017
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4 
5#define CAPACITY 50000 // Size of the Hash Table
6 
7unsigned long hash_function(char* str) {
8    unsigned long i = 0;
9    for (int j=0; str[j]; j++)
10        i += str[j];
11    return i % CAPACITY;
12}
13 
14typedef struct Ht_item Ht_item;
15 
16// Define the Hash Table Item here
17struct Ht_item {
18    char* key;
19    char* value;
20};
21 
22typedef struct HashTable HashTable;
23 
24// Define the Hash Table here
25struct HashTable {
26    // Contains an array of pointers
27    // to items
28    Ht_item** items;
29    int size;
30    int count;
31};
32 
33Ht_item* create_item(char* key, char* value) {
34    // Creates a pointer to a new hash table item
35    Ht_item* item = (Ht_item*) malloc (sizeof(Ht_item));
36    item->key = (char*) malloc (strlen(key) + 1);
37    item->value = (char*) malloc (strlen(value) + 1);
38     
39    strcpy(item->key, key);
40    strcpy(item->value, value);
41 
42    return item;
43}
44 
45HashTable* create_table(int size) {
46    // Creates a new HashTable
47    HashTable* table = (HashTable*) malloc (sizeof(HashTable));
48    table->size = size;
49    table->count = 0;
50    table->items = (Ht_item**) calloc (table->size, sizeof(Ht_item*));
51    for (int i=0; i<table->size; i++)
52        table->items[i] = NULL;
53 
54    return table;
55}
56 
57void free_item(Ht_item* item) {
58    // Frees an item
59    free(item->key);
60    free(item->value);
61    free(item);
62}
63 
64void free_table(HashTable* table) {
65    // Frees the table
66    for (int i=0; i<table->size; i++) {
67        Ht_item* item = table->items[i];
68        if (item != NULL)
69            free_item(item);
70    }
71 
72    free(table->items);
73    free(table);
74}
75 
76void handle_collision(HashTable* table, unsigned long index, Ht_item* item) {
77}
78 
79void ht_insert(HashTable* table, char* key, char* value) {
80    // Create the item
81    Ht_item* item = create_item(key, value);
82 
83    // Compute the index
84    unsigned long index = hash_function(key);
85 
86    Ht_item* current_item = table->items[index];
87     
88    if (current_item == NULL) {
89        // Key does not exist.
90        if (table->count == table->size) {
91            // Hash Table Full
92            printf("Insert Error: Hash Table is full\n");
93            // Remove the create item
94            free_item(item);
95            return;
96        }
97         
98        // Insert directly
99        table->items[index] = item; 
100        table->count++;
101    }
102 
103    else {
104            // Scenario 1: We only need to update value
105            if (strcmp(current_item->key, key) == 0) {
106                strcpy(table->items[index]->value, value);
107                return;
108            }
109     
110        else {
111            // Scenario 2: Collision
112            // We will handle case this a bit later
113            handle_collision(table, index, item);
114            return;
115        }
116    }
117}
118 
119char* ht_search(HashTable* table, char* key) {
120    // Searches the key in the hashtable
121    // and returns NULL if it doesn't exist
122    int index = hash_function(key);
123    Ht_item* item = table->items[index];
124 
125    // Ensure that we move to a non NULL item
126    if (item != NULL) {
127        if (strcmp(item->key, key) == 0)
128            return item->value;
129    }
130    return NULL;
131}
132 
133void print_search(HashTable* table, char* key) {
134    char* val;
135    if ((val = ht_search(table, key)) == NULL) {
136        printf("Key:%s does not exist\n", key);
137        return;
138    }
139    else {
140        printf("Key:%s, Value:%s\n", key, val);
141    }
142}
143 
144void print_table(HashTable* table) {
145    printf("\nHash Table\n-------------------\n");
146    for (int i=0; i<table->size; i++) {
147        if (table->items[i]) {
148            printf("Index:%d, Key:%s, Value:%s\n", i, table->items[i]->key, table->items[i]->value);
149        }
150    }
151    printf("-------------------\n\n");
152}
153 
154int main() {
155    HashTable* ht = create_table(CAPACITY);
156    ht_insert(ht, "1", "First address");
157    ht_insert(ht, "2", "Second address");
158    print_search(ht, "1");
159    print_search(ht, "2");
160    print_search(ht, "3");
161    print_table(ht);
162    free_table(ht);
163    return 0;
164}
165
queries leading to this page
hash in chash code c implementationimplemetn ash table in c using arraystruct hashtable c implementationhow to make a hash table of structs in chasing strinngs eiht hash tble in chash tables chow to make a hashtable in chashtable simple cc program to implement hash tables with hash tablehash code example in chash table code in chash search function in cc hash table sample codc hash table examplehasmap in chash table using arrays in cc hash tablehashing code in chashtable in chash table example c codehash table algorithm for strings in cimplementing a hash table in cimplement hashing in chashes in chashmap in c 3bstruct hashtable chash table in data structure in c in implementhasthtable in chashtable implementation in chash table c codec hastableare there inbuilt functions for hashtable in chash table implementation chash implementation in cc hash table sample codec hashhow to implement hash table in chashtable example chash table c implementationhash table chow to make hash maps in chahmp in csource code in c hash buckethash table 28part i 29implementation in chash map in chashing types starting with cc hash tab 27ec hashtablehashing table in ccreating a hashtable in chash table c indexhashtable cc implement hash tablehash table in chash table example chash table data structure implementation in chash table c programmingcreating hash address chash function chash algorithm in chashmap in chashing using array in chash tables in ccreate a hash map in chow to implement hashtable in chash table c code examplecreate hash coded in csource code in c hash bucket