Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- const int TABLE_SIZE = 100;
- int hashFunction(const std::string& key) {
- int hashValue = 0;
- for (char ch : key) {
- hashValue = (hashValue * 29 + ch) % TABLE_SIZE;
- }
- return hashValue;
- }
- struct Pair {
- std::string key;
- };
- class HashTable {
- private:
- Pair table[TABLE_SIZE];
- public:
- void insert(const std::string& key) {
- int index = hashFunction(key);
- while (!table[index].key.empty()) {
- index = (index + 1) % TABLE_SIZE;
- }
- table[index].key = key;
- }
- bool search(const std::string& key) {
- int index = hashFunction(key);
- while (!table[index].key.empty()) {
- if (table[index].key == key) //
- return true;
- index = (index + 1) % TABLE_SIZE; //
- }
- return false;
- }
- };
- int main() {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement