#include #include using namespace std; // Definition der Hashtabelle const int TABLE_SIZE = 10; class HashNode { public: int key; int value; HashNode(int key, int value) : key(key), value(value) {} }; class HashTable { private: vector table; public: HashTable() { table.resize(TABLE_SIZE, nullptr); } // Hashfunktion int hashFunction(int key) { return key % TABLE_SIZE; } // Schlüssel-Wert-Paar in die Hashtabelle einfügen void insert(int key, int value) { int index = hashFunction(key); HashNode* newNode = new HashNode(key, value); // Kollisionsbehandlung mit offenen Adressierungsstrategie (linear probing) while (table[index] != nullptr) { index = (index + 1) % TABLE_SIZE; } table[index] = newNode; } // Wert anhand des Schlüssels aus der Hashtabelle abrufen int get(int key) { int index = hashFunction(key); // Kollisionsbehandlung und Suche nach dem Schlüssel while (table[index] != nullptr && table[index]->key != key) { index = (index + 1) % TABLE_SIZE; } // Überprüfen, ob der Schlüssel gefunden wurde if (table[index] != nullptr && table[index]->key == key) { return table[index]->value; } // Wenn der Schlüssel nicht gefunden wurde return -1; } // Schlüssel aus der Hashtabelle löschen void remove(int key) { int index = hashFunction(key); // Kollisionsbehandlung und Suche nach dem Schlüssel while (table[index] != nullptr && table[index]->key != key) { index = (index + 1) % TABLE_SIZE; } // Überprüfen, ob der Schlüssel gefunden wurde if (table[index] != nullptr && table[index]->key == key) { delete table[index]; table[index] = nullptr; } } ~HashTable() { for (int i = 0; i < TABLE_SIZE; i++) { if (table[i] != nullptr) { delete table[i]; table[i] = nullptr; } } } }; int main() { HashTable ht; // Einige Schlüssel-Wert-Paare einfügen ht.insert(5, 10); ht.insert(15, 20); ht.insert(25, 30); // Wert anhand des Schlüssels abrufen cout << "Value at key 5: " << ht.get(5) << endl; cout << "Value at key 15: " << ht.get(15) << endl; cout << "Value at key 25: " << ht.get(25) << endl; // Schlüssel aus der Hashtabelle löschen ht.remove(15); // Überprüfen, ob der Schlüssel entfernt wurde cout << "Value at key 15 after removal: " << ht.get(15) << endl; return 0; }