Advertisement
Val_Kir

хэш таблицы наташи

Jul 2nd, 2020
1,045
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. #include "time.h"
  3. #include "string"
  4. using namespace std;
  5.  
  6. int coun;
  7.  
  8. class List
  9. {
  10.     class Node //узел(эл-т)
  11.     {
  12.     public:
  13.         Node* pNext;
  14.         char* data;
  15.  
  16.         Node(char* data = nullptr, Node* pNext = nullptr)
  17.         {
  18.             this->data = data;
  19.             this->pNext = pNext;
  20.         }
  21.     };
  22.     Node* head;
  23.     int size;
  24. public:
  25.     List()
  26.     {
  27.         size = 0;
  28.         head = nullptr;
  29.     }
  30.  
  31.     ~List()
  32.     {
  33.         while (size)
  34.         {
  35.             popfront();
  36.         }
  37.     }
  38.  
  39.     //проверяем наличие строки(есть такая или нет)
  40.     bool compare(char* key)
  41.     {
  42.         Node* current = head;
  43.         while (current)
  44.         {
  45.             coun++;
  46.             if (strcmp(current->data, key) == 0) // обе строки равны
  47.             {
  48.                 return false;
  49.             }
  50.             current = current->pNext;
  51.         }
  52.         return true;
  53.     }
  54.     //удаление первого элемента в списке
  55.     void popfront()
  56.     {
  57.         Node* temp = head;
  58.         head = head->pNext;
  59.         delete temp;
  60.         size--;
  61.     }
  62.     //добавление элемента в конец списка
  63.     void pushback(char* data)
  64.     {
  65.         if (head == nullptr)
  66.         {
  67.             //создаем новый элемент и передаем данные в конструктор
  68.             head = new Node(data);
  69.         }
  70.         else
  71.         {
  72.             Node* current = this->head;
  73.  
  74.             // идем по элементам, до тех пор пока не найдем элемент с указателем на NULL
  75.             while (current->pNext != nullptr)
  76.             {
  77.                 current = current->pNext;
  78.             }
  79.             current->pNext = new Node(data); // создаем новый элемент и присваем адрес
  80.         }
  81.         size++;
  82.     }
  83. };
  84.  
  85. class Table
  86. {
  87.     List* tab;
  88.     int q;
  89.  
  90.     // вычисление хеш-функции по схеме Горнера
  91.     int hashFunction(char* key)
  92.     {
  93.         int i = 0;
  94.         for (int j = 0; j < strlen(key); j++)
  95.         {
  96.             i = i * 31 + key[j];
  97.             i = i % q;
  98.         }
  99.         return i;
  100.     }
  101. public:
  102.  
  103.     // Конструктор с параметром q(простое число)
  104.     Table(int q)
  105.     {
  106.         this->q = q;
  107.         tab = new List[q];
  108.     }
  109.  
  110.     // Деструктор
  111.     ~Table()
  112.     {
  113.         delete[] tab;
  114.     }
  115.  
  116.     // Вставка
  117.     void insert(char* key)
  118.     {
  119.         int c = hashFunction(key);
  120.  
  121.         if (tab[c].compare(key))
  122.         {
  123.             tab[c].pushback(key);
  124.         }
  125.     }
  126. };
  127.  
  128. int main()
  129. {
  130.     setlocale(LC_ALL, "rus");
  131.     srand(time(0));
  132.     int N, q, l;
  133.  
  134.     cout << "Введите значение N" << endl;
  135.     scanf_s("%d", &N);
  136.     cout << "Введите значение q (>=N)" << endl;
  137.     scanf_s("%d", &q);
  138.     cout << "Введите длину строки" << endl;
  139.     scanf_s("%d", &l);
  140.  
  141.     Table  t(q);
  142.     for (int i = 0; i < N - 1; i++)
  143.     {
  144.         char* s = new char[l + 1];
  145.         for (int j = 0; j < l; j++)
  146.         {
  147.             s[j] = (char('a' + rand() % 3));
  148.         }
  149.         s[l] = '\0';
  150.  
  151.         t.insert(s);
  152.     }
  153.     cout << endl << "Количество сравнений: " << coun << endl;
  154.  
  155.     return 0;
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement