Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <memory>
- #include <list>
- using namespace std;
- class LinkedMap
- {
- struct Node
- {
- string key;
- string value;
- shared_ptr<Node> next;
- shared_ptr<Node> prev;
- };
- static shared_ptr<Node> createNode(string key, string value)
- {
- shared_ptr<Node> newNode = make_shared<Node>();
- newNode->key = key;
- newNode->value = value;
- newNode->prev = nullptr;
- newNode->next = nullptr;
- return newNode;
- }
- vector<list<shared_ptr<Node>>> data;
- shared_ptr<Node> first;
- shared_ptr<Node> last;
- unsigned hash(string key)
- {
- unsigned hash = 0;
- int charCode;
- const int k = 199;
- for (char i : key)
- {
- charCode = tolower(i) - 'a';
- hash = (hash * k + charCode) % data.size();
- }
- return hash;
- }
- public:
- LinkedMap(unsigned size)
- {
- data.resize(size);
- }
- void put(string key, string value)
- {
- unsigned index = hash(key);
- for (shared_ptr<Node> &i : data[index])
- {
- if (i->key == key)
- {
- i->value = value;
- return;
- }
- }
- shared_ptr<Node> NodeToPut = createNode(key, value);
- NodeToPut->prev = last;
- if (last != nullptr)
- last->next = NodeToPut;
- last = NodeToPut;
- if (first == nullptr)
- first = NodeToPut;
- data[index].push_back(NodeToPut);
- }
- string get(string key)
- {
- unsigned index = hash(key);
- for (shared_ptr<Node> i : data[index])
- {
- if (i->key == key)
- return i->value;
- }
- return "none";
- }
- void remove(string key)
- {
- unsigned index = hash(key);
- for (shared_ptr<Node> &i : data[index])
- {
- if (i->key == key)
- {
- if (i->prev != nullptr)
- i->prev->next = i->next;
- else
- first = i->next;
- if (i->next != nullptr)
- i->next->prev = i->prev;
- else
- last = i->prev;
- data[index].remove(i);
- return;
- }
- }
- }
- string prev(string key)
- {
- unsigned index = hash(key);
- for (shared_ptr<Node> i : data[index])
- {
- if (i->key == key)
- {
- if (i->prev != nullptr)
- return i->prev->value;
- else
- return "none";
- }
- }
- return "none";
- }
- string next(string key)
- {
- unsigned index = hash(key);
- for (shared_ptr<Node> i : data[index])
- {
- if (i->key == key)
- {
- if (i->next != nullptr)
- return i->next->value;
- else
- return "none";
- }
- }
- return "none";
- }
- };
- int main()
- {
- LinkedMap lmap(100003);
- ifstream inp("linkedmap.in");
- ofstream out("linkedmap.out");
- string command, x, y;
- while (!inp.eof())
- {
- command = "";
- inp >> command >> x;
- // cout << "Before:\n";
- // lmap.print();
- if (command == "put")
- {
- inp >> y;
- lmap.put(x, y);
- }
- else if (command == "get")
- {
- out << lmap.get(x) << '\n';
- }
- else if (command == "delete")
- {
- lmap.remove(x);
- }
- else if (command == "prev")
- {
- out << lmap.prev(x) << '\n';
- }
- else if (command == "next")
- {
- out << lmap.next(x) << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement