Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define SIZE (int)1e+4 + 7
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. using namespace std;
  7.  
  8. struct Node {
  9. string key;
  10. string val;
  11. bool exs;
  12. Node *prev;
  13. Node *next;
  14. } *last = NULL;
  15.  
  16. vector <vector <Node> > hash_t(SIZE);
  17.  
  18. int hashFunc(string key) {
  19. int p = 137;
  20. int p_pow = 1;
  21. int hash = 0;
  22. for (size_t i = 0; i < key.size(); i++) {
  23. hash += (int)key[i] * p_pow;
  24. p_pow *= p; p_pow = abs(p_pow) % SIZE;
  25. hash %= SIZE;
  26. }
  27. return hash;
  28. }
  29.  
  30. void makePair(string key, string val);
  31. void deleteKey(string key);
  32. void getVal(string key);
  33. void prevVal(string key);
  34. void nextVal(string key);
  35.  
  36. int main() {
  37. ios_base::sync_with_stdio(false);
  38.  
  39. freopen("linkedmap.in", "r", stdin);
  40. freopen("linkedmap.out", "w", stdout);
  41.  
  42. string operation, key, value;
  43. while (cin >> operation) {
  44. cin >> key;
  45. if (operation == "put") {
  46. cin >> value;
  47. makePair(key, value);
  48. }
  49. else if (operation == "delete")
  50. deleteKey(key);
  51. else if (operation == "get")
  52. getVal(key);
  53. else if (operation == "prev")
  54. prevVal(key);
  55. else if (operation == "next")
  56. nextVal(key);
  57. }
  58. return 0;
  59. }
  60.  
  61. void makePair(string key, string val) {
  62. int hash = hashFunc(key);
  63. for (size_t i = 0; i < hash_t[hash].size(); i++) {
  64. if (hash_t[hash][i].key == key) {
  65. if (hash_t[hash][i].exs == false) {
  66. hash_t[hash][i].exs = true;
  67. hash_t[hash][i].prev = last;
  68. hash_t[hash][i].next = NULL;
  69. if (last != NULL) {
  70. last->next = &hash_t[hash][i];
  71. }
  72. last = &hash_t[hash][i];
  73. }
  74. hash_t[hash][i].val = val;
  75. return;
  76. }
  77. }
  78. Node *temp = new Node;
  79. hash_t[hash].push_back(*temp);
  80. hash_t[hash].back().key = key;
  81. hash_t[hash].back().val = val;
  82. hash_t[hash].back().exs = true;
  83. hash_t[hash].back().next = NULL;
  84. hash_t[hash].back().prev = last;
  85. if (last != NULL)
  86. last->next = &hash_t[hash].back();
  87. last = &hash_t[hash].back();
  88. delete temp;
  89. }
  90.  
  91. void deleteKey(string key) {
  92. int hash = hashFunc(key);
  93. for (size_t i = 0; i < hash_t[hash].size(); i++) {
  94. if (hash_t[hash][i].key == key) {
  95. if (hash_t[hash][i].exs == true) {
  96. Node *prev = hash_t[hash][i].prev;
  97. Node *next = hash_t[hash][i].next;
  98. if (prev != NULL)
  99. prev->next = next;
  100. if (next != NULL)
  101. next->prev = prev;
  102. if (prev == NULL && next == NULL)
  103. last = NULL;
  104. else if (next == NULL)
  105. last = prev;
  106. hash_t[hash][i].exs = false;
  107. return;
  108. }
  109. return;
  110. }
  111. }
  112. }
  113.  
  114. void getVal(string key) {
  115. int hash = hashFunc(key);
  116. for (size_t i = 0; i < hash_t[hash].size(); i++)
  117. if (hash_t[hash][i].key == key && hash_t[hash][i].exs == true) {
  118. cout << hash_t[hash][i].val << endl;
  119. return;
  120. }
  121. cout << "none" << endl;
  122. }
  123.  
  124. void prevVal(string key) {
  125. int hash = hashFunc(key);
  126. for (size_t i = 0; i < hash_t[hash].size(); i++)
  127. if (hash_t[hash][i].key == key && hash_t[hash][i].exs == true) {
  128. if (hash_t[hash][i].prev != NULL)
  129. cout << hash_t[hash][i].prev->val << endl;
  130. else
  131. cout << "none" << endl;
  132. return;
  133. }
  134. cout << "none" << endl;
  135. }
  136.  
  137. void nextVal(string key) {
  138. int hash = hashFunc(key);
  139. for (size_t i = 0; i < hash_t[hash].size(); i++)
  140. if (hash_t[hash][i].key == key && hash_t[hash][i].exs == true) {
  141. if (hash_t[hash][i].next != NULL)
  142. cout << hash_t[hash][i].next->val << endl;
  143. else
  144. cout << "none" << endl;
  145. return;
  146. }
  147. cout << "none" << endl;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement