From f763184e6e749407a0ee60b30bc345a891a18695 Mon Sep 17 00:00:00 2001 From: Abiral jain <134861989+Abiral-2724@users.noreply.github.com> Date: Fri, 27 Oct 2023 13:37:48 +0530 Subject: [PATCH] Create Visa Q12-LRU cache --- Visa/Visa Q12-LRU cache | 85 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Visa/Visa Q12-LRU cache diff --git a/Visa/Visa Q12-LRU cache b/Visa/Visa Q12-LRU cache new file mode 100644 index 0000000..578501e --- /dev/null +++ b/Visa/Visa Q12-LRU cache @@ -0,0 +1,85 @@ +#include +#include + +using namespace std; + +struct Node { + int key; + int value; + Node* prev; + Node* next; + Node(int k, int v) : key(k), value(v), prev(nullptr), next(nullptr) {} +}; + +class LRUCache { +private: + int capacity; + unordered_map cache; + Node* head; + Node* tail; + + void removeNode(Node* node) { + node->prev->next = node->next; + node->next->prev = node->prev; + } + + void addToFront(Node* node) { + node->next = head->next; + node->prev = head; + head->next->prev = node; + head->next = node; + } + +public: + LRUCache(int capacity) : capacity(capacity) { + head = new Node(-1, -1); + tail = new Node(-1, -1); + head->next = tail; + tail->prev = head; + } + + int get(int key) { + if (cache.find(key) != cache.end()) { + Node* node = cache[key]; + removeNode(node); + addToFront(node); + return node->value; + } else { + return -1; + } + } + + void put(int key, int value) { + if (cache.find(key) != cache.end()) { + Node* node = cache[key]; + removeNode(node); + delete node; + } + + if (cache.size() >= capacity) { + Node* toRemove = tail->prev; + removeNode(toRemove); + cache.erase(toRemove->key); + delete toRemove; + } + + Node* newNode = new Node(key, value); + addToFront(newNode); + cache[key] = newNode; + } +}; + +int main() { + LRUCache lRUCache(2); + lRUCache.put(1, 1); + lRUCache.put(2, 2); + cout << lRUCache.get(1) << endl; // Output: 1 + lRUCache.put(3, 3); + cout << lRUCache.get(2) << endl; // Output: -1 + lRUCache.put(4, 4); + cout << lRUCache.get(1) << endl; // Output: -1 + cout << lRUCache.get(3) << endl; // Output: 3 + cout << lRUCache.get(4) << endl; // Output: 4 + + return 0; +}