Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <functional>
  4. #include <list>
  5.  
  6. using namespace std;
  7.  
  8. class HashTable {
  9. public:
  10. HashTable(int SIZE, function<int(int value)> hashFunction) {
  11. this->SIZE = SIZE;
  12. mainArray = new list<int> * [SIZE];
  13. for (int i = 0; i < SIZE; i++) {
  14. mainArray[i] = new list<int>;
  15. }
  16. this->hashFunction = hashFunction;
  17. }
  18.  
  19. ~HashTable()
  20. {
  21. delete[] mainArray;
  22. }
  23.  
  24. void Add(int value) {
  25. mainArray[hashFunction(value)]->push_back(value);
  26. }
  27.  
  28. void Delete(int value) {
  29. int index = hashFunction(value);
  30. auto it = mainArray[index]->begin();
  31.  
  32. for (auto it = mainArray[index]->begin(); it != mainArray[index]->end(); it++) {
  33. if (*it == value) {
  34. mainArray[index]->erase(it);
  35. break;
  36. }
  37. }
  38. }
  39.  
  40. void Find(int value) {
  41. int index = hashFunction(value);
  42. auto it = mainArray[index]->begin();
  43. bool finded = false;
  44. int pos = 0;
  45.  
  46. for (auto it = mainArray[index]->begin(); it != mainArray[index]->end(); it++) {
  47. if (*it == value) {
  48. finded = true;
  49. break;
  50. }
  51. pos++;
  52. }
  53.  
  54. if (finded) {
  55. cout << "Данное значение хранится в " << hashFunction(value) << " индексе и на " << pos << " позиции" << endl;
  56. }
  57. else {
  58. cout << "Данное значение не найдено!" << endl;
  59. }
  60. }
  61. private:
  62. list<int>** mainArray;
  63. int SIZE;
  64. function<int(int value)> hashFunction;
  65. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement