Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6. template <class T>
  7. class HashTable
  8. {
  9. public:
  10.  
  11.     void Insert(T x)
  12.     {
  13.         int hash = abs(x) % hashsize;
  14.          
  15.         if(!Exist(x))
  16.             hashtable[hash].push_back(x);
  17.     }
  18.  
  19.     void Delete(T x)
  20.     {
  21.         int hash = abs(x) % hashsize;
  22.  
  23.         for (int i = 0; i < hashtable[hash].size(); ++i)
  24.         {
  25.             if (hashtable[hash][i] == x)
  26.                 hashtable[hash].erase(hashtable[hash].begin()+i);
  27.         }
  28.     }
  29.  
  30.     bool Exist(T x)
  31.     {
  32.         int hash = abs(x) % hashsize;
  33.         for (int i = 0; i < hashtable[hash].size(); ++i)
  34.         {
  35.             if (hashtable[hash][i] == x)
  36.                 return true;
  37.         }
  38.         return false;
  39.     }
  40.  
  41. private:
  42.  
  43.     const int hashsize = 1000000;
  44.  
  45.     vector<vector<T>> hashtable = vector<vector<T>>(hashsize,vector<T>());
  46. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement