In the code the hash table I for some reason an error is thrown in Maine, specifically
int main() { struct listnode *node; hashtab_init(hashtab); hashtab_add(hashtab, "Tigr", 190); // error on "Tigr" hashtab_add(hashtab, "Slon", 2300); // error on "Slon" hashtab_add(hashtab, "Volk", 60); // error on "Volk" node = hashtab_lookup(hashtab, "Slon"); // error on "Slon" printf("Node: %s, %d\", node->key, node->value); return 0; }
In code blocks it works.Error (active) E0167 argument of type "const char *" is incompatible with parameter of type "char *"
the code itself.
#include #include #include #include "stdafx.h" #include #include #define HASHTAB_SIZE 71 #define HASHTAB_MUL 31 struct listnode { char *key; int value; struct listnode *next; }; struct listnode *hashtab[HASHTAB_SIZE]; int hashtab_hash(char *key) { int h = 0; char *p; for (p = key; *p != '\\0'; p++) { h = h * HASHTAB_MUL + (int)*p; } return h % HASHTAB_SIZE; } void hashtab_init(struct listnode **hashtab) { int i; for (i = 0; i < HASHTAB_SIZE; i++) { hashtab[i] = NULL; } } int hashtab_add(struct listnode **hashtab, char *key, int value) { struct listnode *node; int index = hashtab_hash(key); node = (struct listnode *) malloc(sizeof(*node)); if (node != NULL) { node->key = key; node->value = value; node->next = hashtab[index]; hashtab[index] = node; } return NULL; } struct listnode *hashtab_lookup( struct listnode **hashtab, char *key) { int index; struct listnode *node; index = hashtab_hash(key); for (node = hashtab[index]; node != NULL; node = node->next) { if (strcmp(node->key, key) == 0) { return node; } } return NULL; } int main() { struct listnode *node; hashtab_init(hashtab); hashtab_add(hashtab, "Tigr", 190); hashtab_add(hashtab, "Slon", 2300); hashtab_add(hashtab, "Volk", 60); node = hashtab_lookup(hashtab, "Slon"); printf("Node: %s, %d\", node->key, node->value); return 0; } void hashtab_delete(struct listnode **hashtab, char *key) { int index; struct listnode *p, *prev = NULL; index = hashtab_hash(key); for (p = hashtab[index]; p != NULL; p = p->next) { if (strcmp(p->key, key) == 0) { if (prev == NULL) hashtab[index] = p->next; else prev->next = p->next; free(p); return; } prev = p; } }