Advertisement
smatskevich

HashTables

Feb 10th, 2022
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <unordered_set>
  4. #include <vector>
  5.  
  6. int Hash(const std::string& key, size_t m) {
  7.   if (key.empty()) return 0;
  8.   return key[0] % m;
  9. }
  10.  
  11. struct HTNode {
  12.   std::string Key;
  13.   HTNode* Next = nullptr;
  14.  
  15.   explicit HTNode(const std::string& key) : Key(key) {}
  16. };
  17.  
  18. // Хеш-таблица строк, разрешающая коллизии методом цепочек.
  19. // Has, Add, Remove.
  20. class HashTable {
  21.  public:
  22.   explicit HashTable(int table_size) : table(table_size, nullptr) {}
  23.  
  24.   bool Has(const std::string& key) const {
  25.     int hash = Hash(key, table.size());
  26.     HTNode* node = table[hash];
  27.     while (node != nullptr) {
  28.       if (node->Key == key) return true;
  29.       node = node->Next;
  30.     }
  31.     return false;
  32.   }
  33.   bool Add(const std::string& key);
  34.   bool Remove(const std::string& key);
  35.  
  36.  private:
  37.   std::vector<HTNode*> table;
  38. };
  39.  
  40. bool HashTable::Add(const std::string& key) {
  41.   // Нужно проверить наличие элемента.
  42.   int hash = Hash(key, table.size());
  43.   HTNode* node = table[hash];
  44.   while (node != nullptr) {
  45.     if (node->Key == key) return false;
  46.     node = node->Next;
  47.   }
  48.   HTNode* new_node = new HTNode(key);
  49.   new_node->Next = table[hash];
  50.   table[hash] = new_node;
  51.   return true;
  52. }
  53.  
  54. // 1 ->  -> **
  55. bool HashTable::Remove(const std::string& key) {
  56.   int hash = Hash(key, table.size());
  57.   HTNode* node = table[hash];
  58.   HTNode* prev = nullptr;
  59.   while (node != nullptr) {
  60.     if (node->Key == key) {
  61.       (prev ? prev->Next : table[hash]) = node->Next;
  62.       delete node;
  63.       return true;
  64.     }
  65.     prev = node;
  66.     node = node->Next;
  67.   }
  68.   return false;
  69. }
  70.  
  71. int main1() {
  72.   HashTable table(80);
  73.   char command = 0;
  74.   std::string value;
  75.   while (std::cin >> command >> value) {
  76.     if (command == '+') {
  77.       std::cout << (table.Add(value) ? "OK" : "FAIL") << std::endl;
  78.     } else if (command == '?') {
  79.       std::cout << (table.Has(value) ? "OK" : "FAIL") << std::endl;
  80.     } else if (command == '-') {
  81.       std::cout << (table.Remove(value) ? "OK" : "FAIL") << std::endl;
  82.     }
  83.   }
  84.   return 0;
  85. }
  86.  
  87. int main() {
  88. /*  int* pointer = new int;
  89.   *pointer = 5;
  90.   std::string s;
  91.   s.length();
  92.   std::string* ps = new std::string("HelloWorld");
  93.   std::cout << *ps;
  94.   std::cout << (*ps).length();
  95.   std::cout << ps->length();*/
  96.  
  97.   std::unordered_set<std::string> table;
  98.   auto it = table.find("Hello");
  99.   it.operator*();
  100. /*  for (auto i = table.begin(); i != table.end(); ++i) {
  101.     std::cout << *it;
  102.   }
  103.   for (const auto& x : table) {
  104.     std::cout << x;
  105.   }*/
  106.   // Новый метод contains есть только в C++20
  107.   char command = 0;
  108.   std::string value;
  109.  
  110.   while (std::cin >> command >> value) {
  111.     if (command == '+') {
  112.       std::cout << (table.insert(value).second ? "OK" : "FAIL") << std::endl;
  113.     } else if (command == '?') {
  114.       std::cout << (table.count(value) ? "OK" : "FAIL") << std::endl;
  115.     } else if (command == '-') {
  116.       std::cout << (table.erase(value) ? "OK" : "FAIL") << std::endl;
  117.     }
  118.   }
  119.   return 0;
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement