mr1302

Untitled

Sep 23rd, 2021
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.78 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. // Структура данных варианта
  8. struct BankAccount {
  9.     int number;
  10.     string name;
  11.     string surname;
  12.     string patronymic;
  13.     string address;
  14. };
  15.  
  16. // Вершина в списке
  17. template<typename T>
  18. struct Node {
  19.     T* data;
  20.     int key;
  21.     Node* next;
  22.     Node(T* data, int key) : data(data), key(key), next(nullptr) {}
  23. };
  24.  
  25. // Класс хеш таблицы
  26. template<typename T>
  27. class HashTable {
  28. private:
  29.     vector<Node<T>*>* table;
  30.     int tableSize = 5;
  31.     int itemsContaining = 0;
  32. public:
  33.     // Конструктор
  34.     HashTable() {
  35.         table = new vector<Node<T>*>(tableSize, nullptr);
  36.     }
  37.  
  38.     // Хэш функция
  39.     int HashFunction(int key, int tableSize) {
  40.         return key % tableSize;
  41.     }
  42.  
  43.     // Добавление вершины в список
  44.     void AddNode(Node<T>& node, T* data, int key) {
  45.         if (!node.next) {
  46.             node.next = new Node<T>(data, key);
  47.             return;
  48.         }
  49.         AddNode(*node.next, data, key);
  50.     }
  51.  
  52.     // Рехеширование
  53.     void TableRehash() {
  54.         int newTableSize = tableSize * 2;
  55.         vector<Node<T>*>* newTable = new vector<Node<T>*>(newTableSize, nullptr);
  56.         for (int i = 0; i < tableSize; ++i) {
  57.             if (!table->operator[](i))
  58.                 continue;
  59.             Node<T>* curNode = table->operator[](i);
  60.             while (true) {
  61.                 int hashCode = HashFunction(curNode->key, newTableSize);
  62.                 if (!newTable->operator[](hashCode))
  63.                     newTable->operator[](hashCode) = new Node<T>(curNode->data, curNode->key);
  64.                 else
  65.                     AddNode(*newTable->operator[](hashCode), curNode->data, curNode->key);
  66.                 if (!curNode->next)
  67.                     break;
  68.                 curNode = curNode->next;
  69.             }
  70.         }
  71.         table = newTable;
  72.         tableSize = newTableSize;
  73.     }
  74.  
  75.     // Добавление элемента
  76.     void Add(T* data, int key) {
  77.         int o = 0;
  78.         int hashCode = HashFunction(key, tableSize);
  79.         if (!table->operator[](hashCode))
  80.             table->operator[](hashCode) = new Node<T>(data, key);
  81.         else
  82.             AddNode(*table->operator[](hashCode), data, key);
  83.         itemsContaining++;
  84.         if ((double)itemsContaining / (double)tableSize > 0.75) {
  85.             TableRehash();
  86.         }
  87.     }
  88.  
  89.     // Возврат таблицы
  90.     vector<T*> GetTable() {
  91.         vector<T*> plainTable;
  92.         for (int i = 0; i < tableSize; ++i) {
  93.             if (!table->operator[](i))
  94.                 continue;
  95.             Node<T>* curNode = table->operator[](i);
  96.             while (true) {
  97.                 plainTable.push_back(curNode->data);
  98.                 if (!curNode->next)
  99.                     break;
  100.                 curNode = curNode->next;
  101.             }
  102.         }
  103.         return plainTable;
  104.     }
  105.  
  106.     // Возврат элемента по ключу
  107.     T* Get(int key){
  108.         int hashCode = HashFunction(key, tableSize);
  109.         Node<T>* curNode = table->operator[](hashCode);
  110.         if(!curNode) return nullptr;
  111.         while (true) {
  112.             if(curNode->key == key)
  113.                 return curNode->data;
  114.             if (!curNode->next)
  115.                 return nullptr;
  116.             curNode = curNode->next;
  117.         }
  118.     }
  119.  
  120.     // Удаление элементов
  121.     void Delete(int key){
  122.         int hashCode = HashFunction(key, tableSize);
  123.         Node<T>* curNode = table->operator[](hashCode);
  124.         if(!curNode)
  125.             return;
  126.         if(!curNode->next && curNode->key == key) {
  127.             delete curNode;
  128.             table->operator[](hashCode) = nullptr;
  129.             return;
  130.         }
  131.         if(curNode->key == key && curNode->next){
  132.             table->operator[](hashCode) = curNode->next;
  133.             delete curNode;
  134.             return;
  135.         }
  136.         while (true) {
  137.             if(curNode->next && curNode->next->key == key){
  138.                 if(curNode->next->next){
  139.                     delete curNode->next;
  140.                     curNode->next = curNode->next->next;
  141.                     return;
  142.                 }
  143.                 if(!curNode->next->next){
  144.                     delete curNode->next;
  145.                     curNode->next = nullptr;
  146.                     return;
  147.                 }
  148.             }
  149.             if (!curNode->next)
  150.                 return;
  151.             curNode = curNode->next;
  152.         }
  153.     }
  154. };
  155.  
  156. int main() {
  157.     HashTable<BankAccount>* a = new  HashTable<BankAccount>();
  158.     int number;
  159.     string name;
  160.     string surname;
  161.     string patronymic;
  162.     string address;
  163.     int command = 0;
  164.     int command_argument = 0;
  165.     BankAccount* ba;
  166.     vector<BankAccount*> b;
  167.     cout << "Enter commands \n(\n\"0 [number of elements to add]\" - add elements\n" <<
  168.          "\"1 [key]\" - get element by key\n\"2 [key]\" - delete element by key\n\"3\" - print full table\n\"4\" - exit\n)\n";
  169.     while(true){
  170.         cout << ">";
  171.         cin >> command;
  172.         switch (command) {
  173.             case 0:
  174.                 cin >> command_argument;
  175.                 cout << "Enter " << command_argument << " elements\n";
  176.                 for(int i = 0; i < command_argument; ++i){
  177.                     cin >> number >> name >> surname >> patronymic >> address;
  178.                     ba = new BankAccount();
  179.                     ba->number = number;
  180.                     ba->name = name;
  181.                     ba->surname = surname;
  182.                     ba->patronymic = patronymic;
  183.                     ba->address = address;
  184.                     a->Add(ba, number);
  185.                 }
  186.                 break;
  187.             case 1:
  188.                 cin >> command_argument;
  189.                 ba = a->Get(command_argument);
  190.                 if(ba)
  191.                     cout << ba->number << " " << ba->name << " " << ba->surname << " " << ba->patronymic << " " << ba->address << '\n';
  192.                 else
  193.                     cout << "No element by key " << command_argument << '\n';
  194.                 break;
  195.             case 2:
  196.                 cin >> command_argument;
  197.                 a->Delete(command_argument);
  198.                 cout << "Element deleted\n";
  199.                 break;
  200.             case 3:
  201.                 b = a->GetTable();
  202.                 for (int i = 0; i < b.size(); ++i) {
  203.                     cout << b[i]->number << " " << b[i]->name << " " << b[i]->surname << " " << b[i]->patronymic << " " << b[i]->address << '\n';
  204.                 }
  205.                 break;
  206.             case 4:
  207.                 cout << "Exiting...";
  208.                 return 0;
  209.             default:
  210.                 cout << "Unknown command\n";
  211.         }
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment