diff --git a/provaEDB/hashTable/hashTable.hpp b/provaEDB/hashTable/hashTable.hpp index 06207fbbef7bb2e8ce8f7d4b285159deb2f81444..5f95275b34445099b1c1991c1373f45e3df6d717 100644 --- a/provaEDB/hashTable/hashTable.hpp +++ b/provaEDB/hashTable/hashTable.hpp @@ -5,6 +5,7 @@ #include <vector> #include <list> #include <iostream> +#include <algorithm> template <typename T> class HashTable { @@ -12,9 +13,9 @@ class HashTable { int tamanho; std::vector<std::list<T>> buckets; public: - HashTable(int tamanho = 10){ + HashTable(int tamanho = 5){ this->buckets.resize(tamanho); - tamanho = 0; + this->tamanho = 0; } bool vazio(){ @@ -26,20 +27,20 @@ class HashTable { } void print(){ - for(auto& b : buckets){ - for(auto l : b){ - std::cout << l << " "; + for(auto& bucket : buckets){ + std::cout << "["; + for(auto valor : bucket){ + std::cout << " " << valor << " "; } + std::cout << "]"; std::cout << std::endl; } } bool find(T valor){ - int idx = hash(valor) % - getCapacidade() == 0 ? - 1 : getCapacidade(); + int idx = hash(valor) % getCapacidade(); - int posicao = std::find( + auto posicao = std::find( buckets[idx].begin(), buckets[idx].end(), valor @@ -49,12 +50,8 @@ class HashTable { } void adicionar(T valor) { - int idx = hash(valor) % - getCapacidade() == 0 ? - 1 : getCapacidade(); - std::cout << "hue" << '\n'; + int idx = hash(valor) % getCapacidade(); buckets[idx].push_back(valor); - } // Gets e sets diff --git a/provaEDB/questao4/bin/exec b/provaEDB/questao4/bin/exec index 74b9b3a6b7b5a7834c200e76c086fcb3cac39a6c..8f7fb27aab7a4e1d4625525f4195af3dcdd69a53 100755 Binary files a/provaEDB/questao4/bin/exec and b/provaEDB/questao4/bin/exec differ diff --git a/provaEDB/questao4/build/main.o b/provaEDB/questao4/build/main.o index a62c953b12b1336b89e9618ea6eb93d627226fb6..c55255be5efdcef0cbcf582008ad36424aec1dc4 100644 Binary files a/provaEDB/questao4/build/main.o and b/provaEDB/questao4/build/main.o differ diff --git a/provaEDB/questao4/src/main.cpp b/provaEDB/questao4/src/main.cpp index e18b2790bb9feaa38aba3cb3c654af343c2fa8e8..c75cb184d61e6464c613a093efe58b5e5ab37cba 100644 --- a/provaEDB/questao4/src/main.cpp +++ b/provaEDB/questao4/src/main.cpp @@ -4,14 +4,17 @@ int main(){ HashTable <int> h; - h.adicionar(1); - h.adicionar(2); - h.adicionar(3); - h.adicionar(4); - h.adicionar(4); - + h.adicionar(11); + h.adicionar(20); + h.adicionar(18); + h.adicionar(49); + h.adicionar(15); h.print(); + std::cout << h.find(15) << std::endl; + std::cout << h.find(49) << std::endl; + std::cout << h.find(55) << std::endl; + return 0; }