Advertisement
Guest User

Untitled

a guest
May 25th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<cstdio>
  5. using namespace std;
  6. const int TABLE_SIZE = 8;
  7.  
  8. /*
  9. * HashNode Class Declaration
  10. */
  11. class HashNode
  12. {
  13. public:
  14. int key;
  15. int value;
  16. HashNode* next;
  17.  
  18. HashNode(int key, int value)//конструктор
  19. {
  20. this->key = key;
  21. this->value = value;
  22. this->next = NULL;
  23. }
  24. };
  25.  
  26. /*
  27. * HashMap Class Declaration
  28. */
  29. class HashMap
  30. {
  31. private:
  32. HashNode** htable;
  33. public:
  34. HashMap()//конструктор
  35. {
  36. htable = new HashNode*[TABLE_SIZE];
  37. for (int i = 0; i < TABLE_SIZE; i++)
  38. htable[i] = NULL;
  39. }
  40. ~HashMap()//диструктор(при удалении)
  41. {
  42. for (int i = 0; i < TABLE_SIZE; ++i)
  43. {
  44. HashNode* entry = htable[i];
  45. while (entry != NULL)
  46. {
  47. HashNode* prev = entry;
  48. entry = entry->next;
  49. delete prev;
  50. }
  51. }
  52. delete[] htable;
  53. }
  54. /*
  55. * Hash Function
  56. */
  57. int HashFunc(int key)
  58. {
  59. return key % TABLE_SIZE;
  60. }
  61.  
  62. /*
  63. * Add Element at a key
  64. */
  65. void Insert(int key, int value)
  66. {
  67. int hash_val = HashFunc(key);
  68. HashNode* prev = NULL;
  69. HashNode* entry = htable[hash_val];
  70. while (entry != NULL)
  71. {
  72. prev = entry;
  73. entry = entry->next;
  74. }
  75. if (entry == NULL)
  76. {
  77. entry = new HashNode(key, value);
  78. if (prev == NULL)
  79. {
  80. htable[hash_val] = entry;
  81. }
  82. else
  83. {
  84. prev->next = entry;
  85. }
  86. }
  87. else
  88. {
  89. entry->value = value;
  90. }
  91. }
  92. /*
  93. * Delete Element at a key
  94. */
  95. void del(int value)
  96. {
  97. bool found = false;
  98. for (int i = 0; i < TABLE_SIZE && !found; i++)
  99. {
  100. HashNode* temp = htable[i];
  101. HashNode* prev = NULL;
  102. while (temp)
  103. {
  104. if (temp->value == value)
  105. {
  106. if (!prev)
  107. {
  108. htable[i] = temp->next;
  109. delete temp;
  110. found = true;
  111. break;
  112. }
  113. else
  114. {
  115. prev->next = temp->next;
  116. delete temp;
  117. found = true;
  118. break;
  119. }
  120. }
  121. prev = temp;
  122. temp = temp->next;
  123. }
  124. }
  125. if (!found)
  126. cout << "Не найден" << endl;
  127. else
  128. cout << "Успешно удалено" << endl;
  129. system("pause");
  130. }
  131. /*
  132. * Search Element at a key
  133. */
  134. int Search(int key)
  135. {
  136. bool flag = false;
  137. int hash_val = HashFunc(key);
  138. HashNode* entry = htable[hash_val];
  139. while (entry != NULL)
  140. {
  141. if (entry->key == key)
  142. {
  143. cout<<entry->value<<" ";
  144. flag = true;
  145. }
  146. entry = entry->next;
  147. }
  148. if (!flag)
  149. return -1;
  150. }
  151. /*
  152. * Print
  153. */
  154.  
  155. void print()
  156. {
  157. for (int i = 0;i<8; i++)
  158. {
  159. cout << "Ключ " << i <<": ";
  160. HashNode* temp = htable[i];
  161. while (temp)
  162. {
  163. cout << temp->value << " ";
  164. temp = temp->next;
  165. }
  166. cout << endl;
  167. }
  168. }
  169. }
  170. ;
  171. /*
  172. * Main
  173. */
  174. int main()
  175. {
  176. setlocale(LC_ALL, "russian");
  177. HashMap hash;
  178. int key, value;
  179. int choice;
  180. while (1)
  181. {
  182. cout<<"\n----------------------"<<endl;
  183. cout<<"Операции"<<endl;
  184. cout<<"\n----------------------"<<endl;
  185. cout<<"1.Добавить элемент в таблицу"<<endl;
  186. cout<<"2.Поиск по ключу"<<endl;
  187. cout<<"3.Удаление по ключу"<<endl;
  188. cout <<"4.Вывод таблицы"<<endl;
  189. cout<<"5.Выход"<<endl;
  190. cout<<"Ваш выбор: ";
  191. cin>>choice;
  192. switch(choice)
  193. {
  194. case 1:
  195. cout<<"Введите элемент,который хотите добавить: ";
  196. cin>>value;
  197. cout<<"Введите ключ: ";
  198. cin>>key;
  199. hash.Insert(key, value);
  200. break;
  201. case 2:
  202. cout<<"Введите ключ элемента для поиска: ";
  203. cin>>key;
  204. cout<<"Элементы по ключу "<<key<<" : ";
  205. if (hash.Search(key) == -1)
  206. {
  207. cout<<"Элементы по заданному ключу не найдены "<<key<<endl;
  208. continue;
  209. }
  210. break;
  211. case 3:
  212. cout<<"Введите ключ, по которому будет удален элемент: ";
  213. cin>>key;
  214. hash.del(key);
  215. break;
  216. case 4:
  217. cout<<"Хеш-таблица:\n";
  218. hash.print();
  219. break;
  220.  
  221. case 5:
  222. exit(1);
  223. }
  224. }
  225. return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement