Modified search function to return data pointers and changed print function

This commit is contained in:
Harun Teper 2020-06-24 16:24:26 +02:00
parent 014b381086
commit ce8b258204
2 changed files with 7 additions and 6 deletions

View File

@ -27,7 +27,7 @@ public:
~HashTable(); ~HashTable();
void insert(int id, T* n); void insert(int id, T* n);
bool search(int id); T* search(int id);
bool remove(int id); bool remove(int id);
void restructure(); void restructure();
@ -77,16 +77,15 @@ int HashTable<T>::hash(int id) {
} }
template < typename T > template < typename T >
bool HashTable<T>::search(int id) { T* HashTable<T>::search(int id) {
int index = hash(id); int index = hash(id);
std::vector<elem> list = table.at(index).list; std::vector<elem> list = table.at(index).list;
for (auto it = list.begin(); it != list.end(); ++it) { for (auto it = list.begin(); it != list.end(); ++it) {
if (it->key == id) { if (it->key == id) {
return true; return it->data;
} }
} }
return nullptr;
return false;
} }
template < typename T > template < typename T >
@ -129,7 +128,7 @@ void HashTable<T>::print() {
for (auto it = table.begin(); it != table.end(); ++it) { for (auto it = table.begin(); it != table.end(); ++it) {
std::cout << "Next list" << std::endl; std::cout << "Next list" << std::endl;
for (auto it2 = it->list.begin(); it2 != it->list.end(); ++it2) { for (auto it2 = it->list.begin(); it2 != it->list.end(); ++it2) {
std::cout << it2->data << std::endl; std::cout << it2->key << std::endl;
} }
} }

View File

@ -35,6 +35,8 @@ int main() {
table.remove(30); table.remove(30);
std::cout << table.search(30) << std::endl; std::cout << table.search(30) << std::endl;
std::cout << table.search(21) << std::endl;
std::cout << table.search(20) << std::endl;
table.remove(40); table.remove(40);