I'm trying to implement a basic hashtable. I'm using a LinkedList to resolve collisions.
My get and set methods are giving me a bunch of trouble, and I'm not really sure what the issue is. I believe I'm overloading the operator's correctly, and I think my LinkedList append functionality is broken.
class HashTable {
struct Node{
int key;
int value;
Node *next;
};
Node **table;
int hash_func(int key) const {
return key % TABLE_SIZE;
}
public:
HashTable() {
table = new Node*[TABLE_SIZE]();
for (int i = 0; i < TABLE_SIZE; ++i)
table[i] = nullptr;
}
int& operator[](int const key) {
int h_key = hash_func(key);
while(table[h_key]) {
table[h_key] = table[h_key]->next;
}
table[h_key] = new Node;
table[h_key]->key = key;
table[h_key]->next = table[h_key];
return table[h_key]->value;
}
int operator[](int const key) const {
int h_key = hash_func(key);
while (table[h_key]) {
if (table[h_key]->key == key) {
return table[h_key]->value;
}
table[h_key] = table[h_key]->next;
}
return 0;
}
};
Aucun commentaire:
Enregistrer un commentaire