From ce8b258204d81ce15994bdeeea9061dff73d76d9 Mon Sep 17 00:00:00 2001 From: Harun Teper Date: Wed, 24 Jun 2020 16:24:26 +0200 Subject: [PATCH] Modified search function to return data pointers and changed print function --- HashTabelle.h | 11 +++++------ Quelle.cpp | 2 ++ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/HashTabelle.h b/HashTabelle.h index 424b73c..1f039b4 100644 --- a/HashTabelle.h +++ b/HashTabelle.h @@ -27,7 +27,7 @@ public: ~HashTable(); void insert(int id, T* n); - bool search(int id); + T* search(int id); bool remove(int id); void restructure(); @@ -77,16 +77,15 @@ int HashTable::hash(int id) { } template < typename T > -bool HashTable::search(int id) { +T* HashTable::search(int id) { int index = hash(id); std::vector list = table.at(index).list; for (auto it = list.begin(); it != list.end(); ++it) { if (it->key == id) { - return true; + return it->data; } } - - return false; + return nullptr; } template < typename T > @@ -129,7 +128,7 @@ void HashTable::print() { for (auto it = table.begin(); it != table.end(); ++it) { std::cout << "Next list" << std::endl; for (auto it2 = it->list.begin(); it2 != it->list.end(); ++it2) { - std::cout << it2->data << std::endl; + std::cout << it2->key << std::endl; } } diff --git a/Quelle.cpp b/Quelle.cpp index 1ab493c..5f850b2 100644 --- a/Quelle.cpp +++ b/Quelle.cpp @@ -35,6 +35,8 @@ int main() { table.remove(30); std::cout << table.search(30) << std::endl; + std::cout << table.search(21) << std::endl; + std::cout << table.search(20) << std::endl; table.remove(40);