Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef struct list_element {
  6. string key;
  7. string value;
  8. struct list_element* next;
  9. struct list_element* prev;
  10. struct list_element* next_in_seq;
  11. } list_element;
  12.  
  13. class List
  14. {
  15. list_element *s;
  16. public:
  17. List();
  18. void insert(const string& key, const string& value, list_element *head);
  19. void del(const string& key);
  20. list_element* exists(const string& key);
  21. };
  22.  
  23. List::List()
  24. {
  25. s = new list_element();
  26. s->value = s->key = "";
  27. s->next_in_seq = nullptr;
  28. }
  29.  
  30. void List::insert(const string& key, const string& value, list_element *head)
  31. {
  32. list_element *new_element = exists(key);
  33. if (exists(key) == nullptr)
  34. {
  35. new_element = new list_element;
  36. new_element->key = key;
  37. new_element->value = value;
  38. new_element->next_in_seq = s->next_in_seq;
  39. s->next_in_seq = new_element;
  40. list_element *Tail = head->prev;
  41. Tail->next = new_element;
  42. head->prev = new_element;
  43. new_element->next = head;
  44. new_element->prev = Tail;
  45. }
  46. else
  47. {
  48. new_element->value = value;
  49. }
  50. }
  51.  
  52. void List::del(const string& key)
  53. {
  54. list_element *current_element = s;
  55. while (current_element->next_in_seq != nullptr)
  56. {
  57. if (current_element->next_in_seq->key == key)
  58. {
  59. list_element *to_delete = current_element->next_in_seq;
  60. current_element->next_in_seq = current_element->next_in_seq->next_in_seq;
  61. list_element *prev_element = to_delete->prev, *NextNode = to_delete->next;
  62. to_delete->prev->next = NextNode;
  63. to_delete->next->prev = prev_element;
  64. delete to_delete;
  65. return;
  66. }
  67. else
  68. {
  69. current_element = current_element->next_in_seq;
  70. }
  71. }
  72. }
  73.  
  74. list_element* List::exists(const string& key)
  75. {
  76. list_element *current_element = s;
  77. while (current_element->next_in_seq != nullptr)
  78. {
  79. if (current_element->next_in_seq->key == key)
  80. {
  81. return current_element->next_in_seq;
  82. }
  83. else
  84. {
  85. current_element = current_element->next_in_seq;
  86. }
  87. }
  88. return nullptr;
  89. }
  90.  
  91. class LinkedMap
  92. {
  93. vector <List> linkmap;
  94. list_element *head;
  95. static int to_hash(const string& key);
  96. public:
  97. LinkedMap();
  98. void insert(const string& key, const string& value);
  99. void del(const string& key);
  100. void exists(const string& key);
  101. void next(const string& K);
  102. void prev(const string& key);
  103. };
  104.  
  105. LinkedMap::LinkedMap()
  106. {
  107. linkmap.resize(1000001);
  108. head = new list_element;
  109. head->prev = head->next = head;
  110. }
  111.  
  112. int LinkedMap::to_hash(const string &key) {
  113. int hash_value = 0;
  114. for(char i : key) {
  115. hash_value = 41 * hash_value + i;
  116. }
  117. return abs(hash_value % 1000001);
  118. }
  119.  
  120. void LinkedMap::insert(const string& key, const string& value)
  121. {
  122. linkmap[to_hash(key)].insert(key, value, head);
  123. }
  124.  
  125. void LinkedMap::exists(const string& key)
  126. {
  127. list_element *result = linkmap[to_hash(key)].exists(key);
  128. if (result == nullptr)
  129. cout<<"none\n";
  130. else
  131. cout << result->value <<"\n";
  132. }
  133.  
  134. void LinkedMap::del(const string& key)
  135. {
  136. linkmap[to_hash(key)].del(key);
  137. }
  138.  
  139. void LinkedMap::next(const string& K)
  140. {
  141. list_element *result = linkmap[to_hash(K)].exists(K);
  142. if (result == nullptr || result->next == head)
  143. cout<<"none\n";
  144. else
  145. cout<<result->next->value<<"\n";
  146. }
  147.  
  148. void LinkedMap::prev(const string& key)
  149. {
  150. list_element *result = linkmap[to_hash(key)].exists(key);
  151. if (result == nullptr || result->prev == head) {
  152. cout<<"none\n";
  153. }
  154. else {
  155. cout<<result->prev->value<<"\n";
  156. }
  157. }
  158.  
  159. int main() {
  160. freopen("linkedmap.in", "r", stdin);
  161. freopen("linkedmap.out", "w", stdout);
  162. string key;
  163. string value;
  164. string command;
  165. LinkedMap new_map;
  166. while(cin>>command) {
  167. if (command == "put")
  168. {
  169. cin>>key>>value;
  170. new_map.insert(key, value);
  171. }
  172. if (command == "get")
  173. {
  174. cin>>key;
  175. new_map.exists(key);
  176. }
  177. if (command == "delete")
  178. {
  179. cin>>key;
  180. new_map.del(key);
  181. }
  182. if (command == "next")
  183. {
  184. cin>>key;
  185. new_map.next(key);
  186. }
  187. if (command == "prev")
  188. {
  189. cin>>key;
  190. new_map.prev(key);
  191. }
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement