SHARE
TWEET

HashTable Using Class

ltdpaste Jun 16th, 2019 (edited) 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //#include <iostream>
  2. //#include <list>
  3. //#include <fstream>
  4. //#include <vector>
  5. //#include <stdio.h>
  6. //#include <stdlib.h>
  7. //#include <time.h>
  8. //#include <algorithm>
  9. //
  10. //using namespace std;
  11. //
  12. //class Hash
  13. //{
  14. //  int bucket;
  15. //  list<int>* table;  
  16. //public:
  17. //  Hash(int v);
  18. //  void AddItem(int value);
  19. //  void DeleteItem(int key);
  20. //  int HashFunc(int value);
  21. //  void DisplayHash();
  22. //};
  23. //
  24. //Hash::Hash(int v)
  25. //{
  26. //  this->bucket = v;
  27. //  table = new list<int>[bucket];
  28. //}
  29. //
  30. //void Hash::AddItem(int key)
  31. //{
  32. //  int index = HashFunc(key);
  33. //  table[index].push_back(key);
  34. //}
  35. //
  36. //void Hash::DeleteItem(int key)
  37. //{
  38. //  int index = HashFunc(key);
  39. //  list <int>::iterator i;
  40. //  for (i = table[index].begin(); i != table[index].end(); i++)
  41. //  {
  42. //      if (*i == key)
  43. //          break;
  44. //  }
  45. //  if (i != table[index].end())
  46. //      table[index].erase(i);
  47. //}
  48. //
  49. //int Hash::HashFunc(int key)
  50. //{
  51. //  return (key % bucket);
  52. //}
  53. //
  54. //void Hash::DisplayHash()
  55. //{
  56. //  for (int i = 0; i < bucket; i++)
  57. //  {
  58. //      cout << i;
  59. //      for (auto x : table[i])
  60. //      {
  61. //          cout << " --> " << x;
  62. //      }
  63. //      cout << endl;
  64. //  }
  65. //}
  66. //
  67. //void Random(int n)
  68. //{
  69. //  srand(time(NULL));
  70. //  ofstream out;
  71. //  out.open("input.txt");
  72. //  int v;
  73. //  for (int i = 0; i < n; i++)
  74. //  {
  75. //      v = rand() % (100 + 1);
  76. //      if (i != n)
  77. //          out << v << " ";
  78. //      else
  79. //      {
  80. //          out << v;
  81. //      }
  82. //  }
  83. //  out.close();
  84. //}
  85. //
  86. //void Input(Hash hashtable)
  87. //{
  88. //  vector<int> vec;
  89. //  ifstream inp;
  90. //  inp.open("input.txt");
  91. //  int x;
  92. //  while (inp >> x)
  93. //  {
  94. //      vec.push_back(x);
  95. //  }
  96. //  sort(vec.begin(), vec.end());
  97. //  for (auto i = vec.begin(); i != vec.end(); i++)
  98. //  {
  99. //      hashtable.AddItem(*i);
  100. //  }
  101. //}
  102. //
  103. //int main()
  104. //{
  105. //  /*vector<int> vec;
  106. //  for (int i = 0; i < 10; i++)
  107. //      vec.push_back(i);
  108. //  for (auto j = vec.begin(); j != vec.end(); j++)
  109. //      cout << *j << " ";*/
  110. //  int m, n;
  111. //  cout << "Nhap so bucket: ";
  112. //  cin >> m;
  113. //  cout << "Nhap so phan tu can random: ";
  114. //  cin >> n;
  115. //  Random(n);
  116. //  Hash hashtable(m);
  117. //  Input(hashtable);
  118. //  hashtable.DisplayHash();
  119. //  return 0;
  120. //}
  121.  
  122. // ======================================Viết lại=========================================
  123.  
  124. #include <iostream>
  125. #include <fstream>
  126. #include <stdlib.h>
  127. #include <stdio.h>
  128. #include <list>
  129. #include <vector>
  130. #include <algorithm>
  131. #include <time.h>
  132.  
  133. using namespace std;
  134.  
  135. class HashTable
  136. {
  137.     int bucket;
  138.     list<int>* hashtab;
  139. public:
  140.     HashTable(int v);
  141.     int HashFunc(int value);
  142.     void AddItem(int value);
  143.     void DeleteItem(int value);
  144.     void DisplayHash();
  145. };
  146.  
  147. HashTable::HashTable(int v)
  148. {
  149.     this->bucket = v;
  150.     hashtab = new list<int>[bucket];
  151. }
  152.  
  153. int HashTable::HashFunc(int value)
  154. {
  155.     return (value % bucket);
  156. }
  157.  
  158. void HashTable::AddItem(int value)
  159. {
  160.     int index = HashFunc(value);
  161.     hashtab[index].push_back(value);
  162. }
  163.  
  164. void HashTable::DeleteItem(int value)
  165. {
  166.     int index = HashFunc(value);
  167.     list<int> ::iterator i;
  168.     for (i = hashtab[index].begin(); i != hashtab[index].end(); i++)
  169.     {
  170.         if (*i == value)
  171.             break;
  172.     }
  173.     if (i != hashtab[index].end())
  174.         hashtab[index].erase(i);
  175. }
  176.  
  177. void HashTable::DisplayHash()
  178. {
  179.     for (int i = 0; i < bucket; i++)
  180.     {
  181.         cout << i;
  182.         for (auto x : hashtab[i])
  183.         {
  184.             cout << "-->" << x;
  185.         }
  186.         cout << endl;
  187.     }
  188. }
  189.  
  190. void Input(HashTable hash)
  191. {
  192.     ifstream inp;
  193.     int x;
  194.     inp.open("input.txt");
  195.     vector<int> vec;
  196.     if (!inp.is_open())
  197.         cout << "ERROR: Cannot open the input file!";  
  198.     else
  199.     {
  200.         while (inp >> x)
  201.         {
  202.             vec.push_back(x);
  203.         }
  204.     }
  205.     sort(vec.begin(), vec.end());
  206.     for (auto i = vec.begin(); i != vec.end(); i++)
  207.     {
  208.         hash.AddItem(*i);
  209.     }
  210. }
  211.  
  212. int main()
  213. {
  214.     int m;
  215.     cout << "Nhap so bucket: ";
  216.     cin >> m;
  217.     HashTable Hash(m);
  218.     Input(Hash);
  219.     Hash.DeleteItem(27);
  220.     Hash.DeleteItem(99);
  221.     Hash.DisplayHash();
  222.     return 0;
  223. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top