Advertisement
Marisichka

Untitled

Dec 5th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. class HashTable {
  7. private:
  8.     static const int hashGroups = 10; //количество листов, которое используем
  9.     list<pair<int, string>> table[hashGroups]; //пара будет хранить ключ
  10.     // List 1, Index 0, List 2, Index 1 ...
  11.  
  12. public:
  13.     bool isEmpty() const;
  14.     int hashFunction(int key);
  15.     void insertItem(int key, string value);
  16.     void removeItem(int key);
  17.     string searchTable(int key);
  18.     void printTable();
  19. };
  20.  
  21. bool HashTable::isEmpty() const {
  22.     int sum{};
  23.     for (int i{}; i < hashGroups; i++) {
  24.         sum += table[i].size();//Ищем размер
  25.     }
  26.  
  27.     if (!sum) {
  28.         return true;
  29.     }
  30.     return false;
  31. }
  32.  
  33. int HashTable::hashFunction(int key) {
  34.     return key % hashGroups; //Key: 905, in return, this function will spit on 5.
  35. }
  36.  
  37. void HashTable::insertItem(int key, string value) {
  38.     int hashValue = hashFunction(key);
  39.     auto& cell = table[hashValue];
  40.     auto bItr = begin(cell);
  41.     bool keyExists = false;
  42.     for (; bItr != end(cell); bItr++) {
  43.         if (bItr->first == key) {
  44.             keyExists = true;
  45.             bItr->second = value;
  46.             cout << endl << "[WARNING] Key exists. Value replaced." << endl << endl;
  47.             break;
  48.         }
  49.     }
  50.  
  51.     if (!keyExists) {
  52.         cell.emplace_back(key, value);
  53.     }
  54.     return;
  55. }
  56.  
  57. void HashTable::removeItem(int key) {
  58.     int hashValue = hashFunction(key);
  59.     auto& cell = table[hashValue];
  60.     auto bItr = begin(cell);
  61.     bool keyExists = false;
  62.     for (; bItr != end(cell); bItr++) {
  63.         if (bItr->first == key) {
  64.             keyExists = true;
  65.             bItr = cell.erase(bItr);
  66.             cout << "[INFO] Item removed." << endl;
  67.             break;
  68.         }
  69.     }
  70.  
  71.     if (!keyExists) {
  72.         cout << "[WARNING] Key is not found. Pair is not removed." << endl;
  73.     }
  74.     return;
  75. }
  76.  
  77. void HashTable::printTable() {
  78.     for (int i{}; i < hashGroups; i++) {
  79.         if (table[i].size() == 0) continue;
  80.  
  81.         auto bItr = table[i].begin();
  82.         for (; bItr != table[i].end(); bItr++) {
  83.             cout << "[INFO] Key: " << bItr->first << " Value: " << bItr->second << endl;
  84.         }
  85.     }
  86.     return;
  87. }
  88.  
  89. int main() {
  90.     HashTable HT;
  91.  
  92.     if (HT.isEmpty()) {
  93.         cout << "Correct answer. Good job. ";
  94.     }
  95.     else {
  96.         cout << "Oh, we need to check out code." << endl;
  97.     }
  98.  
  99.     HT.insertItem(905, "Jim");
  100.     HT.insertItem(201, "Tom");
  101.     HT.insertItem(332, "Bob");
  102.     HT.insertItem(124, "Sally");
  103.     HT.insertItem(107, "Sandy");
  104.     HT.insertItem(929, "Barb");
  105.     HT.insertItem(928, "Rob");
  106.     HT.insertItem(928, "Rick");
  107.  
  108.     HT.printTable();
  109.  
  110.     HT.removeItem(332);
  111.     HT.removeItem(100);
  112.  
  113.     if (HT.isEmpty()) {
  114.         cout << "Oh, we need to check out code." << endl;
  115.     }
  116.     else {
  117.         cout << endl << "Correct answer. Good job. " << endl;
  118.     }
  119.  
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement