Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <memory>
  6. #include <list>
  7.  
  8. using namespace std;
  9.  
  10. class LinkedMap
  11. {
  12.     struct Node
  13.     {
  14.         string key;
  15.         string value;
  16.         shared_ptr<Node> next;
  17.         shared_ptr<Node> prev;
  18.     };
  19.  
  20.     static shared_ptr<Node> createNode(string key, string value)
  21.     {
  22.         shared_ptr<Node> newNode = make_shared<Node>();
  23.         newNode->key = key;
  24.         newNode->value = value;
  25.         newNode->prev = nullptr;
  26.         newNode->next = nullptr;
  27.         return newNode;
  28.     }
  29.  
  30.     vector<list<shared_ptr<Node>>> data;
  31.  
  32.     shared_ptr<Node> first;
  33.     shared_ptr<Node> last;
  34.  
  35.     unsigned hash(string key)
  36.     {
  37.         unsigned hash = 0;
  38.         int charCode;
  39.         const int k = 199;
  40.         for (char i : key)
  41.         {
  42.             charCode = tolower(i) - 'a';
  43.             hash = (hash * k + charCode) % data.size();
  44.         }
  45.         return hash;
  46.     }
  47.  
  48. public:
  49.     LinkedMap(unsigned size)
  50.     {
  51.         data.resize(size);
  52.     }
  53.  
  54.     void put(string key, string value)
  55.     {
  56.         unsigned index = hash(key);
  57.         for (shared_ptr<Node> &i : data[index])
  58.         {
  59.             if (i->key == key)
  60.             {
  61.                 i->value = value;
  62.                 return;
  63.             }
  64.         }
  65.         shared_ptr<Node> NodeToPut = createNode(key, value);
  66.         NodeToPut->prev = last;
  67.         if (last != nullptr)
  68.             last->next = NodeToPut;
  69.         last = NodeToPut;
  70.         if (first == nullptr)
  71.             first = NodeToPut;
  72.         data[index].push_back(NodeToPut);
  73.     }
  74.  
  75.     string get(string key)
  76.     {
  77.         unsigned index = hash(key);
  78.         for (shared_ptr<Node> i : data[index])
  79.         {
  80.             if (i->key == key)
  81.                 return i->value;
  82.         }
  83.         return "none";
  84.     }
  85.  
  86.     void remove(string key)
  87.     {
  88.         unsigned index = hash(key);
  89.         for (shared_ptr<Node> &i : data[index])
  90.         {
  91.             if (i->key == key)
  92.             {
  93.                 if (i->prev != nullptr)
  94.                     i->prev->next = i->next;
  95.                 else
  96.                     first = i->next;
  97.                 if (i->next != nullptr)
  98.                     i->next->prev = i->prev;
  99.                 else
  100.                     last = i->prev;
  101.                 data[index].remove(i);
  102.                 return;
  103.             }
  104.         }
  105.     }
  106.  
  107.     string prev(string key)
  108.     {
  109.         unsigned index = hash(key);
  110.         for (shared_ptr<Node> i : data[index])
  111.         {
  112.             if (i->key == key)
  113.             {
  114.                 if (i->prev != nullptr)
  115.                     return i->prev->value;
  116.                 else
  117.                     return "none";
  118.             }
  119.         }
  120.         return "none";
  121.     }
  122.  
  123.     string next(string key)
  124.     {
  125.         unsigned index = hash(key);
  126.         for (shared_ptr<Node> i : data[index])
  127.         {
  128.             if (i->key == key)
  129.             {
  130.                 if (i->next != nullptr)
  131.                     return i->next->value;
  132.                 else
  133.                     return "none";
  134.             }
  135.         }
  136.         return "none";
  137.     }
  138. };
  139.  
  140. int main()
  141. {
  142.     LinkedMap lmap(100003);
  143.     ifstream inp("linkedmap.in");
  144.     ofstream out("linkedmap.out");
  145.     string command, x, y;
  146.     while (!inp.eof())
  147.     {
  148.         command = "";
  149.         inp >> command >> x;
  150.         //        cout << "Before:\n";
  151.         //        lmap.print();
  152.         if (command == "put")
  153.         {
  154.             inp >> y;
  155.             lmap.put(x, y);
  156.         }
  157.         else if (command == "get")
  158.         {
  159.             out << lmap.get(x) << '\n';
  160.         }
  161.         else if (command == "delete")
  162.         {
  163.             lmap.remove(x);
  164.         }
  165.         else if (command == "prev")
  166.         {
  167.             out << lmap.prev(x) << '\n';
  168.         }
  169.         else if (command == "next")
  170.         {
  171.             out << lmap.next(x) << '\n';
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement