Skip to content
Snippets Groups Projects
Commit e3eacfa2 authored by Ítalo Epifânio de Lima e Silva's avatar Ítalo Epifânio de Lima e Silva
Browse files

Resolve prova de edbII

parent e40eb5f1
Branches provaEDBII
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <vector> #include <vector>
#include <list> #include <list>
#include <iostream> #include <iostream>
#include <algorithm>
template <typename T> template <typename T>
class HashTable { class HashTable {
...@@ -12,9 +13,9 @@ class HashTable { ...@@ -12,9 +13,9 @@ class HashTable {
int tamanho; int tamanho;
std::vector<std::list<T>> buckets; std::vector<std::list<T>> buckets;
public: public:
HashTable(int tamanho = 10){ HashTable(int tamanho = 5){
this->buckets.resize(tamanho); this->buckets.resize(tamanho);
tamanho = 0; this->tamanho = 0;
} }
bool vazio(){ bool vazio(){
...@@ -26,20 +27,20 @@ class HashTable { ...@@ -26,20 +27,20 @@ class HashTable {
} }
void print(){ void print(){
for(auto& b : buckets){ for(auto& bucket : buckets){
for(auto l : b){ std::cout << "[";
std::cout << l << " "; for(auto valor : bucket){
std::cout << " " << valor << " ";
} }
std::cout << "]";
std::cout << std::endl; std::cout << std::endl;
} }
} }
bool find(T valor){ bool find(T valor){
int idx = hash(valor) % int idx = hash(valor) % getCapacidade();
getCapacidade() == 0 ?
1 : getCapacidade();
int posicao = std::find( auto posicao = std::find(
buckets[idx].begin(), buckets[idx].begin(),
buckets[idx].end(), buckets[idx].end(),
valor valor
...@@ -49,12 +50,8 @@ class HashTable { ...@@ -49,12 +50,8 @@ class HashTable {
} }
void adicionar(T valor) { void adicionar(T valor) {
int idx = hash(valor) % int idx = hash(valor) % getCapacidade();
getCapacidade() == 0 ?
1 : getCapacidade();
std::cout << "hue" << '\n';
buckets[idx].push_back(valor); buckets[idx].push_back(valor);
} }
// Gets e sets // Gets e sets
......
No preview for this file type
No preview for this file type
...@@ -4,14 +4,17 @@ ...@@ -4,14 +4,17 @@
int main(){ int main(){
HashTable <int> h; HashTable <int> h;
h.adicionar(1); h.adicionar(11);
h.adicionar(2); h.adicionar(20);
h.adicionar(3); h.adicionar(18);
h.adicionar(4); h.adicionar(49);
h.adicionar(4); h.adicionar(15);
h.print(); 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; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment