Advertisement
MKbear

SiAOD

Sep 25th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct  Node {
  6.     int num;
  7.     string com;
  8.     string secname;
  9.     Node* next;
  10.     Node(int _num, string _com, string _secname) : num(_num), com(_com), secname(_secname), next(nullptr) {}
  11. };
  12.  
  13. struct list {
  14.     Node* first;
  15.     Node* last;
  16.     list() : first(nullptr), last(nullptr) {}
  17.     bool is_empty() {
  18.         return first == nullptr;
  19.     }
  20.     void push_back(int _num, string _com, string _secname) {
  21.         Node* p = new Node(_num, _com, _secname);
  22.         if (is_empty()) {
  23.             first = p;
  24.             last = p;
  25.             return;
  26.         }
  27.         last->next = p;
  28.         last = p;
  29.     }
  30.  
  31.     void print() {
  32.         if (is_empty()) return;
  33.         Node* p = first;
  34.         while (p) {
  35.             cout << p->num << "|" << p->com << "|" << p->secname << " ---->";
  36.             p = p->next;
  37.         }
  38.         cout << endl;
  39.     }
  40.  
  41.     Node* find(int _num) {
  42.         Node* p = first;
  43.         while (p && p->num != _num) p = p->next;
  44.         cout << p->num << "|" << p->com << "|" << p->secname;
  45.         return (p && p->num == _num) ? p : nullptr;
  46.     }
  47.  
  48.     void remove_first() {
  49.         if (is_empty()) return;
  50.         Node* p = first;
  51.         first = p->next;
  52.         delete p;
  53.     }
  54.  
  55.     void remove_last() {
  56.         if (is_empty()) return;
  57.         if (first == last) {
  58.             remove_first();
  59.             return;
  60.         }
  61.         Node* p = first;
  62.         while (p->next != last) p = p->next;
  63.         p->next = nullptr;
  64.         delete last;
  65.         last = p;
  66.     }
  67.  
  68.     void remove(int _num) {
  69.         if (is_empty()) return;
  70.         if (first->num == _num) {
  71.             remove_first();
  72.             return;
  73.         }
  74.         else if (last->num == _num) {
  75.             remove_last();
  76.             return;
  77.         }
  78.         Node* slow = first;
  79.         Node* fast = first->next;
  80.         while (fast && fast->num != _num) {
  81.             fast = fast->next;
  82.             slow = slow->next;
  83.         }
  84.         if (!fast) {
  85.             cout << "This element does not exist" << endl;
  86.             return;
  87.         }
  88.         slow->next = fast->next;
  89.         delete fast;
  90.     }
  91. };
  92.  
  93. class HashTable {
  94. private:
  95.  
  96.     list* l = new list[20];
  97.     int Num;
  98.     string Fio;
  99.     string Adress;
  100.  
  101. public:
  102.  
  103.     HashTable() {
  104.         for (int i = 0; i < 5; i++) {
  105.             cin >> Num >> Fio >> Adress;
  106.             int id = hashFunction(Num); // Находим индекс элемента в массиве
  107.             cout << id;
  108.             l[id].push_back(Num, Fio, Adress); // добавляем в список
  109.         }
  110.     }
  111.  
  112.     int hashFunction(int _num) {
  113.         return _num % 10;
  114.     }
  115.  
  116.     void printTable() { // метод вывода таблицы
  117.  
  118.         for (int i = 0; i < 20; i++) {
  119.             l[i].print();
  120.         }
  121.         cout << endl;
  122.     }
  123.  
  124.     void insertItem(int _num, string _com, string _secname) { // метод вставки элементов
  125.  
  126.         int id = hashFunction(_num);
  127.         l[id].push_back(_num, _com, _secname);
  128.  
  129.     }
  130.  
  131.     void deleteItem(int _num) { // метод удаления элементов
  132.  
  133.         int id = hashFunction(_num);
  134.  
  135.         l[id].remove(_num);
  136.  
  137.     }
  138.  
  139.     void findItem(int _num) { // метод поиска элементов
  140.  
  141.         int id = hashFunction(_num);
  142.         l[id].find(_num);
  143.         cout << endl;
  144.     }
  145. };
  146.  
  147. int main()
  148.  
  149. {
  150.     int a, b, c;
  151.     HashTable hash;
  152.  
  153.     hash.printTable();
  154.     cin >> a;
  155.     cout << "Insert" << endl;
  156.     hash.insertItem(a, "5", "7");
  157.     hash.printTable();
  158.     cin >> b;
  159.     cout << "Find" << endl;
  160.     hash.findItem(b);
  161.     cin >> c;
  162.     cout << "Delete" << endl;
  163.     hash.deleteItem(c);
  164.     hash.printTable();
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement