Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. struct ListElement
  7. {
  8. string key, value;
  9. ListElement *chain;
  10. ListElement *next_element, *previous_element;
  11. };
  12.  
  13. class List
  14. {
  15. ListElement *newlist;
  16. public:
  17. List();
  18. void Insert(const string& Key, const string& Value, ListElement *Head);
  19. void Delete(const string& Key, ListElement *Head);
  20. ListElement* Search(const string& Key);
  21. };
  22.  
  23. List::List()
  24. {
  25. newlist = new ListElement();
  26. newlist->value = newlist->key = "";
  27. newlist->chain = nullptr;
  28. }
  29.  
  30. void List::Insert(const string& Key, const string& Value, ListElement *Head)
  31. {
  32. ListElement *NewNode = Search(Key);
  33. if (Search(Key) == nullptr)
  34. {
  35. NewNode = new ListElement;
  36. NewNode->key = Key;
  37. NewNode->value = Value;
  38. NewNode->chain = newlist->chain;
  39. newlist->chain = NewNode;
  40. ListElement *Tail = Head->previous_element;
  41. Tail->next_element = NewNode;
  42. Head->previous_element = NewNode;
  43. NewNode->next_element = Head;
  44. NewNode->previous_element = Tail;
  45. }
  46. else
  47. {
  48. NewNode->value = Value;
  49. }
  50. }
  51.  
  52. void List::Delete(const string& Key, ListElement *Head)
  53. {
  54. ListElement *CurNode = newlist;
  55. while (CurNode->chain != nullptr)
  56. {
  57. if (CurNode->chain->key == Key)
  58. {
  59. ListElement *NodeToDel = CurNode->chain;
  60. CurNode->chain = CurNode->chain->chain;
  61. ListElement *PrevNode = NodeToDel->previous_element, *NextNode = NodeToDel->next_element;
  62. NodeToDel->previous_element->next_element = NextNode;
  63. NodeToDel->next_element->previous_element = PrevNode;
  64. delete NodeToDel;
  65. return;
  66. }
  67. else
  68. {
  69. CurNode = CurNode->chain;
  70. }
  71. }
  72. }
  73.  
  74. ListElement* List::Search(const string& Key)
  75. {
  76. ListElement *current = newlist;
  77. while (current->chain != nullptr)
  78. {
  79. if (current->chain->key == Key)
  80. {
  81. return current->chain;
  82. }
  83. else
  84. {
  85. current = current->chain;
  86. }
  87. }
  88. return nullptr;
  89. }
  90.  
  91. class LinkedMap
  92. {
  93. vector<List> source;
  94. ListElement *Head;
  95. int StringToHash(string Key);
  96. public:
  97. LinkedMap();
  98. void Insert(const string& Key, const string& Value);
  99. void Delete(const string& Key);
  100. string Search(const string& Key);
  101. string Next(const string& Key);
  102. string Prev(const string& Key);
  103. };
  104.  
  105. LinkedMap::LinkedMap()
  106. {
  107. source.resize(1000);
  108. Head = new ListElement;
  109. Head->previous_element = Head->next_element = Head;
  110. }
  111.  
  112. int LinkedMap::StringToHash(string Key)
  113. {
  114. unsigned int res = 0, pow = 1, p = 31, s = source.size();
  115. for (int i = 0; i < Key.length(); ++i)
  116. {
  117. res += (Key[i] - 'A') * pow;
  118. pow *= p;
  119. }
  120. return res % s;
  121. }
  122.  
  123. void LinkedMap::Insert(const string& Key, const string& Value)
  124. {
  125. source[StringToHash(Key)].Insert(Key, Value, Head);
  126. }
  127.  
  128. string LinkedMap::Search(const string& Key)
  129. {
  130. ListElement *Res = source[StringToHash(Key)].Search(Key);
  131. if (Res == nullptr)
  132. return "none";
  133. else
  134. return Res->value;
  135. }
  136.  
  137. void LinkedMap::Delete(const string& Key)
  138. {
  139. source[StringToHash(Key)].Delete(Key, Head);
  140. }
  141.  
  142. string LinkedMap::Next(const string& Key)
  143. {
  144. ListElement *Res = source[StringToHash(Key)].Search(Key);
  145. if (Res == nullptr || Res->next_element == Head)
  146. return "none";
  147. else
  148. return Res->next_element->value;
  149. }
  150.  
  151. string LinkedMap::Prev(const string& Key)
  152. {
  153. ListElement *Res = source[StringToHash(Key)].Search(Key);
  154. if (Res == nullptr || Res->previous_element == Head)
  155. return "none";
  156. else
  157. return Res->previous_element->value;
  158. }
  159.  
  160. int main()
  161. {
  162. ifstream fin("linkedmap.in");
  163. ofstream fout("linkedmap.out");
  164. LinkedMap LM;
  165. string command;
  166. while (fin >> command)
  167. {
  168. string K, V;
  169. if (command == "put")
  170. {
  171. fin >> K >> V;
  172. LM.Insert(K, V);
  173. }
  174. else if (command == "delete")
  175. {
  176. fin >> K;
  177. LM.Delete(K);
  178. }
  179. else if (command == "get")
  180. {
  181. fin >> K;
  182. fout << LM.Search(K) << "\n";
  183. }
  184. else if (command == "next")
  185. {
  186. fin >> K;
  187. fout << LM.Next(K) << "\n";
  188. }
  189. else if (command == "prev")
  190. {
  191. fin >> K;
  192. fout << LM.Prev(K) << "\n";
  193. }
  194. }
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement