Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. struct Element {
  7. int key;
  8. struct Element* link;
  9. };
  10.  
  11. Element* hashTable[115249] = {nullptr};
  12.  
  13. void insert(int key) {
  14. int index = abs(key % 115249);
  15. Element* ptr = new Element();
  16. ptr->key = key;
  17. ptr->link = nullptr;
  18. if (hashTable[index] == nullptr) {
  19. hashTable[index] = ptr;
  20. }
  21. else {
  22. Element* ptr1;
  23. ptr1 = hashTable[index];
  24. while (ptr1->link) {
  25. if (ptr1->key == key)
  26. return;
  27. ptr1 = ptr1->link;
  28. }
  29. if (ptr1->key != key)
  30. ptr1->link = ptr;
  31. }
  32. }
  33.  
  34. void del(int key) {
  35. Element* ptr;
  36. int index = abs(key % 115249);
  37. ptr = hashTable[index];
  38. if (ptr == nullptr)
  39. return;
  40. if (ptr->key == key) {
  41. hashTable[index] = ptr->link;
  42. return;
  43. }
  44. while (ptr->link) {
  45. if (ptr->link->key == key) {
  46. ptr->link = ptr->link->link;
  47. return;
  48. }
  49. ptr = ptr->link;
  50. }
  51. }
  52.  
  53. bool exists(int key) {
  54. Element* ptr;
  55. int index = abs(key % 115249);
  56. ptr = hashTable[index];
  57. if (!ptr) {
  58. return false;
  59. }
  60. if (ptr->key == key) {
  61. return true;
  62. }
  63.  
  64. while (ptr->link) {
  65. if (ptr->key == key)
  66. return true;
  67. ptr = ptr->link;
  68. }
  69. return ptr->key == key;
  70. }
  71.  
  72.  
  73.  
  74. int main() {
  75. ifstream fin("set.in");
  76. ofstream fout("set.out");
  77. string com;
  78. int key;
  79. while (fin >> com) {
  80. fin >> key;
  81. if (com == "insert")
  82. insert(key);
  83. else if (com == "delete")
  84. del(key);
  85. else if (com == "exists"){
  86. bool ex = exists(key);
  87. if (ex)
  88. fout << "true" << "\n";
  89. else
  90. fout << "false" << "\n";
  91. }
  92. }
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement