Created
November 2, 2021 21:37
-
-
Save danielomiya/4963efb60c02edacf374fa03d94656ac to your computer and use it in GitHub Desktop.
Hash table implementation in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "hashtable.hpp" | |
| #include <sstream> | |
| HashTable::HashTable(int size) { | |
| int i; | |
| count = 0; | |
| colisions = 0; | |
| capacity = size; | |
| array = new int *[capacity]; | |
| for (i = 0; i < capacity; i++) { | |
| array[i] = nullptr; | |
| } | |
| } | |
| HashTable::~HashTable() { | |
| int i; | |
| for (i = 0; i < capacity; i++) | |
| if (array[i] != nullptr) | |
| delete array[i]; | |
| delete[] array; | |
| } | |
| int HashTable::getColisions() const { return colisions; } | |
| bool HashTable::isFull() const { return count == capacity; } | |
| int HashTable::insert(int data, bool *didCollide) { | |
| int key; | |
| bool hasCollided = false; | |
| if (isFull()) | |
| return -1; | |
| key = hash(data, capacity); | |
| while (hasKey(key)) { | |
| hasCollided = true; | |
| key = (key + 1) % capacity; | |
| } | |
| if (hasCollided) { | |
| ++colisions; | |
| if (didCollide != nullptr) | |
| *didCollide = hasCollided; | |
| } | |
| ++count; | |
| array[key] = new int(data); | |
| return key; | |
| } | |
| bool HashTable::contains(int data) const { | |
| auto key = hash(data, capacity); | |
| auto it = key; | |
| while (array[it] != nullptr) { | |
| if (*array[it] == data) | |
| return true; | |
| it = (it + 1) % capacity; | |
| if (key == it) | |
| /* exit circular infinite loop */ | |
| break; | |
| } | |
| return false; | |
| } | |
| std::string HashTable::toString() const { | |
| int i; | |
| std::stringstream ss; | |
| ss << "[ "; | |
| for (i = 0; i < capacity; i++) { | |
| if (array[i] == nullptr) { | |
| ss << "empty"; | |
| } else { | |
| ss << *array[i]; | |
| } | |
| ss << ' '; | |
| } | |
| ss << ']'; | |
| return ss.str(); | |
| } | |
| bool HashTable::hasKey(int key) const { | |
| auto index = hash(key, capacity); | |
| return array[index] != nullptr; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <string> | |
| inline int hash(int n, int size) { return n % size; } | |
| class HashTable { | |
| private: | |
| int colisions; | |
| int count; | |
| int capacity; | |
| int **array; | |
| protected: | |
| bool hasKey(int key) const; | |
| public: | |
| HashTable(int size); | |
| ~HashTable(); | |
| int getColisions() const; | |
| bool isFull() const; | |
| int insert(int data, bool *didCollide = nullptr); | |
| bool contains(int data) const; | |
| std::string toString() const; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "hashtable.hpp" | |
| #include <iostream> | |
| using std::cerr; | |
| using std::cin; | |
| using std::cout; | |
| using std::endl; | |
| int main() { | |
| HashTable *table; | |
| int option, size, value, position; | |
| bool didCollided; | |
| cout << "Digite o tamanho da tabela hash: "; | |
| cin >> size; | |
| cout << endl; | |
| table = new HashTable(size); | |
| do { | |
| /* Table current state */ | |
| cout << table->toString() << endl; | |
| cout << "Total de colisoes: " << table->getColisions() << endl << endl; | |
| /* User 'interface' */ | |
| cout << "Lista de opcoes disponiveis: " << endl; | |
| cout << "1. Inserir valor" << endl; | |
| cout << "2. Verificar se a tabela contem um valor" << endl; | |
| cout << "0. Sair" << endl << endl; | |
| cout << "Digite a opcao desejada: "; | |
| cin >> option; | |
| switch (option) { | |
| case 0: | |
| /* exiting loop */ | |
| break; | |
| case 1: | |
| cout << "Digite o valor que deseja inserir: "; | |
| cin >> value; | |
| position = table->insert(value, &didCollided); | |
| if (position >= 0) | |
| cout << "Valor inserido " << (didCollided ? "com" : "sem") | |
| << " colisao com sucesso na posicao " << position << endl; | |
| else | |
| cout << "Valor nao pode ser inserido, porque a tabela esta cheia" | |
| << endl; | |
| break; | |
| case 2: | |
| cout << "Digite o valor a ser buscado: "; | |
| cin >> value; | |
| if (table->contains(value)) | |
| cout << "Valor encontrado!"; | |
| else | |
| cerr << "Valor nao encontrado :("; | |
| cout << endl; | |
| break; | |
| default: | |
| cerr << "Opcao desconhecida, tente novamente!" << endl; | |
| break; | |
| } | |
| cout << endl; | |
| } while (option != 0); | |
| delete table; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment