LRU Cache Implementation using Doubly Linked List
Last Updated :
24 Sep, 2024
Design a data structure that works like a LRU(Least Recently Used) Cache. The LRUCache class has two methods get() and put() which are defined as follows.
- LRUCache (Capacity c): Initialize LRU cache with positive size capacity c.
- get(key): returns the value of the key if it already exists in the cache otherwise returns -1.
- put(key, value): if the key is already present, update its value. If not present, add the key-value pair to the cache. If the cache reaches its capacity it should remove the key-value pair with the lowest priority.
Example:
Input: [LRUCache cache = new LRUCache(2) , put(1 ,1) , put(2 ,2) , get(1) , put(3 ,3) , get(2) , put(4 ,4) , get(1) , get(3) , get(4)]
Output: [1 ,-1, -1, 3, 4]
Explanation: The values mentioned in the output are the values returned by get operations.
- Initialize LRUCache class with capacity = 2.
- cache.put(1, 1): (key, pair) = (1,1) inserted and has the highest priority.
- cache.put(2, 2): (key , pair) = (2,2) inserted and has the highest priority.
- cache.get(1): For key 1, value is 1, so 1 returned and (1,1) moved to the highest priority.
- cache.put(3, 3): Since cache is full, remove least recently used that is (2,2), (3,3) inserted with the highest priority.
- cache.get(2): returns -1 (key 2 not found)
- cache.put(4, 4): Since the cache is full, remove least recently used that is (1,1). (4,5) inserted with the highest priority.
- cache.get(1): return -1 (not found)
- cache.get(3): return 3 , (3,3) will moved to the highest priority.
- cache.get(4): return 4 , (4,4) moved to the highest priority.
Thoughts about Implementation Using Arrays, Hashing and/or Heap
We use an array of triplets, where the items are key, value and priority
get(key) : We linearly search the key. If we find the item, we change priorities of all impacted and make the new item as the highest priority.
put(key): If there is space available, we insert at the end. If not, we linearly search items of the lowest priority and replace that item with the new one. We change priorities of all and make the new item as the highest priority.
Time Complexities of both the operations is O(n)
Can we make both operations in O(1) time? we can think of hashing. With hashing, we can insert, get and delete in O(1) time, but changing priorities would take linear time. We can think of using heap along with hashing for priorities. We can find and remove the least recently used (lowest priority) in O(Log n) time which is more than O(1) and changing priority in the heap would also be required.
Using Doubly Linked List and Hashing
The idea is to keep inserting the key-value pair at the head of the doubly linked list. When a node is accessed or added, it is moved to the head of the list (right after the dummy head node). This marks it as the most recently used. When the cache exceeds its capacity, the node at the tail (right before the dummy tail node) is removed as it represents the least recently used item.
Below is the implementation of the above approach:
C++
// C++ program to implement LRU Least Recently Used)
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
int value;
Node *next;
Node *prev;
Node(int k, int v) {
key = k;
value = v;
next = nullptr;
prev = nullptr;
}
};
// LRU Cache class
class LRUCache
{
public:
// Constructor to initialize the cache with a given capacity
int capacity;
unordered_map<int, Node *> cacheMap;
Node *head;
Node *tail;
LRUCache(int capacity) {
this->capacity = capacity;
head = new Node(-1, -1);
tail = new Node(-1, -1);
head->next = tail;
tail->prev = head;
}
// Function to get the value for a given key
int get(int key) {
if (cacheMap.find(key) == cacheMap.end())
return -1;
Node *node = cacheMap[key];
remove(node);
add(node);
return node->value;
}
// Function to put a key-value pair into the cache
void put(int key, int value) {
if (cacheMap.find(key) != cacheMap.end()) {
Node *oldNode = cacheMap[key];
remove(oldNode);
}
Node *node = new Node(key, value);
cacheMap[key] = node;
add(node);
if (cacheMap.size() > capacity) {
Node *nodeToDelete = tail->prev;
remove(nodeToDelete);
cacheMap.erase(nodeToDelete->key);
}
}
// Add a node right after the head
// (most recently used position)
void add(Node *node) {
Node *nextNode = head->next;
head->next = node;
node->prev = head;
node->next = nextNode;
nextNode->prev = node;
}
// Remove a node from the list
void remove(Node *node) {
Node *prevNode = node->prev;
Node *nextNode = node->next;
prevNode->next = nextNode;
nextNode->prev = prevNode;
}
};
int main(){
LRUCache cache(2);
cache.put(1, 1);
cache.put(2, 2);
cout << cache.get(1) << endl;
cache.put(3, 3);
cout << cache.get(2) << endl;
cache.put(4, 4);
cout << cache.get(1) << endl;
cout << cache.get(3) << endl;
cout << cache.get(4) << endl;
return 0;
}
Java
// Java program to implement LRU Least Recently Used)
import java.util.HashMap;
import java.util.Map;
class Node {
int key;
int value;
Node next;
Node prev;
Node(int key, int value) {
this.key = key;
this.value = value;
this.next = null;
this.prev = null;
}
}
class LRUCache {
private int capacity;
private Map<Integer, Node> cacheMap;
private Node head;
private Node tail;
// Constructor to initialize the cache with a given
// capacity
LRUCache(int capacity) {
this.capacity = capacity;
this.cacheMap = new HashMap<>();
this.head = new Node(-1, -1);
this.tail = new Node(-1, -1);
this.head.next = this.tail;
this.tail.prev = this.head;
}
// Function to get the value for a given key
int get(int key) {
if (!cacheMap.containsKey(key)) {
return -1;
}
Node node = cacheMap.get(key);
remove(node);
add(node);
return node.value;
}
// Function to put a key-value pair into the cache
void put(int key, int value) {
if (cacheMap.containsKey(key)) {
Node oldNode = cacheMap.get(key);
remove(oldNode);
}
Node node = new Node(key, value);
cacheMap.put(key, node);
add(node);
if (cacheMap.size() > capacity) {
Node nodeToDelete = tail.prev;
remove(nodeToDelete);
cacheMap.remove(nodeToDelete.key);
}
}
// Add a node right after the head (most recently used
// position)
private void add(Node node) {
Node nextNode = head.next;
head.next = node;
node.prev = head;
node.next = nextNode;
nextNode.prev = node;
}
// Remove a node from the list
private void remove(Node node) {
Node prevNode = node.prev;
Node nextNode = node.next;
prevNode.next = nextNode;
nextNode.prev = prevNode;
}
}
public class Main {
public static void main(String[] args) {
LRUCache cache = new LRUCache(2);
cache.put(1, 1);
cache.put(2, 2);
System.out.println(cache.get(1));
cache.put(3, 3);
System.out.println(cache.get(2));
cache.put(4, 4);
System.out.println(cache.get(1));
System.out.println(cache.get(3));
System.out.println(cache.get(4));
}
}
Python
# Python program to implement LRU Least Recently Used)
class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.prev = None
self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache = {}
self.head = Node(-1, -1)
self.tail = Node(-1, -1)
self.head.next = self.tail
self.tail.prev = self.head
def _add(self, node: Node):
# Add a node right after the head
# (most recently used position).
next_node = self.head.next
self.head.next = node
node.prev = self.head
node.next = next_node
next_node.prev = node
def _remove(self, node: Node):
# emove a node from the
# doubly linked list.
prev_node = node.prev
next_node = node.next
prev_node.next = next_node
next_node.prev = prev_node
def get(self, key: int) -> int:
# Get the value for a given key
if key not in self.cache:
return -1
node = self.cache[key]
self._remove(node)
self._add(node)
return node.value
def put(self, key: int, value: int):
#Put a key-value pair into the cache.
if key in self.cache:
node = self.cache[key]
self._remove(node)
del self.cache[key]
if len(self.cache) >= self.capacity:
# Remove the least recently used item
# (just before the tail)
lru_node = self.tail.prev
self._remove(lru_node)
del self.cache[lru_node.key]
# Add the new node
new_node = Node(key, value)
self._add(new_node)
self.cache[key] = new_node
if __name__ == "__main__":
cache = LRUCache(2)
cache.put(1, 1)
cache.put(2, 2)
print(cache.get(1))
cache.put(3, 3)
print(cache.get(2))
cache.put(4, 4)
print(cache.get(1))
print(cache.get(3))
print(cache.get(4))
C#
// C# program to implement LRU Least Recently Used)
using System;
using System.Collections.Generic;
class Node {
public int Key;
public int Value;
public Node Prev;
public Node Next;
public Node(int key, int value) {
Key = key;
Value = value;
Prev = null;
Next = null;
}
}
class LRUCache {
private int capacity;
private Dictionary<int, Node> cache;
private Node head;
private Node tail;
// Constructor to initialize the
// cache with a given capacity
public LRUCache(int capacity){
this.capacity = capacity;
cache = new Dictionary<int, Node>();
head = new Node(-1, -1);
tail = new Node(-1, -1);
head.Next = tail;
tail.Prev = head;
}
// Add a node right after the head
//(most recently used position)
private void Add(Node node) {
Node nextNode = head.Next;
head.Next = node;
node.Prev = head;
node.Next = nextNode;
nextNode.Prev = node;
}
// Remove a node from the doubly linked list
private void Remove(Node node) {
Node prevNode = node.Prev;
Node nextNode = node.Next;
prevNode.Next = nextNode;
nextNode.Prev = prevNode;
}
// Get the value for a given key
public int Get(int key) {
if (!cache.ContainsKey(key)) {
return -1;
}
Node node = cache[key];
Remove(node);
Add(node);
return node.Value;
}
// Put a key-value pair into the cache
public void Put(int key, int value) {
if (cache.ContainsKey(key)) {
Node oldNode = cache[key];
Remove(oldNode);
cache.Remove(key);
}
if (cache.Count >= capacity) {
Node lruNode = tail.Prev;
Remove(lruNode);
cache.Remove(lruNode.Key);
}
Node newNode = new Node(key, value);
Add(newNode);
cache[key] = newNode;
}
}
class GfG {
static void Main() {
LRUCache cache = new LRUCache(2);
cache.Put(1, 1);
cache.Put(2, 2);
Console.WriteLine(cache.Get(1));
cache.Put(3, 3);
Console.WriteLine(cache.Get(2));
cache.Put(4, 4);
Console.WriteLine(cache.Get(1));
Console.WriteLine(cache.Get(3));
Console.WriteLine(cache.Get(4));
}
}
JavaScript
// Javascript program to implement LRU Least Recently Used)
class Node {
constructor(key, value) {
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
}
}
class LRUCache {
constructor(capacity) {
this.capacity = capacity;
this.cache = new Map();
this.head = new Node(-1, -1);
this.tail = new Node(-1, -1);
this.head.next = this.tail;
this.tail.prev = this.head;
}
// Add a node right after the head
//(most recently used position)
add(node) {
const nextNode = this.head.next;
this.head.next = node;
node.prev = this.head;
node.next = nextNode;
nextNode.prev = node;
}
// Remove a node from the doubly linked list
remove(node) {
const prevNode = node.prev;
const nextNode = node.next;
prevNode.next = nextNode;
nextNode.prev = prevNode;
}
// Get the value for a given key
get(key) {
if (!this.cache.has(key)) {
return -1;
}
const node = this.cache.get(key);
this.remove(node);
this.add(node);
return node.value;
}
// Put a key-value pair into the cache
put(key, value) {
if (this.cache.has(key)) {
const node = this.cache.get(key);
this.remove(node);
this.cache.delete(key);
}
if (this.cache.size >= this.capacity) {
const lruNode = this.tail.prev;
this.remove(lruNode);
this.cache.delete(lruNode.key);
}
const newNode = new Node(key, value);
this.add(newNode);
this.cache.set(key, newNode);
}
}
const cache = new LRUCache(2);
cache.put(1, 1);
cache.put(2, 2);
console.log(cache.get(1));
cache.put(3, 3);
console.log(cache.get(2));
cache.put(4, 4);
console.log(cache.get(1));
console.log(cache.get(3));
console.log(cache.get(4));
Time Complexity : get(key) – O(1) and put(key, value) – O(1)
Auxiliary Space : O(capacity)
Using Inbuilt Doubly Linked List
The idea is to use inbuilt doubly linked list, it simplifies the implementation by avoiding the need to manually manage a doubly linked list while achieving efficient operations. Example – C++ uses a custom doubly linked list as std::list.
Note: Python’s standard library does not include a built-in doubly linked list implementation. To handle use cases that typically require a doubly linked list, such as efficiently managing elements at both ends of a sequence, Python provides the collections.deque class. While deque stands for double-ended queue, it essentially functions as a doubly linked list with efficient operations on both ends.
Below is the implementation of the above approach:
C++
// C++ program to implement LRU Least Recently Used) using
//Built-in Doubly linked list
#include <bits/stdc++.h>
using namespace std;
class LRUCache {
public:
int capacity;
list<pair<int, int>> List;
// Map from key to list iterator
unordered_map<int, list<pair<int, int>>::iterator> cacheMap;
// Constructor to initialize the
//cache with a given capacity
LRUCache(int capacity) {
this->capacity = capacity;
}
// Function to get the value for a given key
int get(int key) {
auto it = cacheMap.find(key);
if (it == cacheMap.end()) {
return -1;
}
// Move the accessed node to the
//front (most recently used position)
int value = it->second->second;
List.erase(it->second);
List.push_front({key, value});
// Update the iterator in the map
cacheMap[key] = List.begin();
return value;
}
// Function to put a key-value pair into the cache
void put(int key, int value) {
auto it = cacheMap.find(key);
if (it != cacheMap.end()) {
// Remove the old node from the list and map
List.erase(it->second);
cacheMap.erase(it);
}
// Insert the new node at the front of the list
List.push_front({key, value});
cacheMap[key] = List.begin();
// If the cache size exceeds the capacity,
//remove the least recently used item
if (cacheMap.size() > capacity) {
auto lastNode = List.back().first;
List.pop_back();
cacheMap.erase(lastNode);
}
}
};
int main() {
LRUCache cache(2);
cache.put(1, 1);
cache.put(2, 2);
cout << cache.get(1) << endl;
cache.put(3, 3);
cout << cache.get(2) << endl;
cache.put(4, 4);
cout << cache.get(1) << endl;
cout << cache.get(3) << endl;
cout << cache.get(4) << endl;
return 0;
}
Java
// Java program to implement LRU Least Recently Used) using
// Built-in Doubly linked list
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
class LRUCache {
private int capacity;
// Stores key-value pairs
private Map<Integer, Integer> cacheMap;
// Stores keys in the order of access
private LinkedList<Integer> lruList;
// Constructor to initialize the cache with a given
// capacity
LRUCache(int capacity) {
this.capacity = capacity;
this.cacheMap = new HashMap<>();
this.lruList = new LinkedList<>();
}
// Function to get the value for a given key
public int get(int key) {
if (!cacheMap.containsKey(key)) {
return -1;
}
// Move the accessed key to the front (most recently
// used position)
lruList.remove(Integer.valueOf(key));
// Add key to the front
lruList.addFirst(key);
return cacheMap.get(key);
}
// Function to put a key-value pair into the cache
public void put(int key, int value) {
if (cacheMap.containsKey(key)) {
// Update the value
cacheMap.put(key, value);
// Move the accessed key to the front
lruList.remove(Integer.valueOf(key));
}
else {
// Add new key-value pair
if (cacheMap.size() >= capacity) {
// Remove the least recently used item
int leastUsedKey = lruList.removeLast();
cacheMap.remove(leastUsedKey);
}
cacheMap.put(key, value);
}
// Add the key to the front (most recently used
// position)
lruList.addFirst(key);
}
public static void main(String[] args) {
LRUCache cache = new LRUCache(2);
cache.put(1, 1);
cache.put(2, 2);
System.out.println(cache.get(1));
cache.put(3, 3);
System.out.println(cache.get(2));
cache.put(4, 4);
System.out.println(cache.get(1));
System.out.println(cache.get(3));
System.out.println( cache.get(4));
}
}
Python
# Python program to implement LRU Least Recently Used) using
# Built-in Doubly linked list
from collections import deque
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
# Dictionary to store key-value pairs
self.cache = {}
# Deque to maintain the order of keys
self.order = deque()
def get(self, key: int) -> int:
if key in self.cache:
# Move the accessed key to
# the front of the deque
self.order.remove(key)
self.order.appendleft(key)
return self.cache[key]
else:
return -1
def put(self, key: int, value: int):
if key in self.cache:
# Update the value and move
# the key to the front
self.cache[key] = value
self.order.remove(key)
self.order.appendleft(key)
else:
if len(self.cache) >= self.capacity:
# Remove the least recently used item
lru_key = self.order.pop()
del self.cache[lru_key]
# Add the new key-value pair
self.cache[key] = value
self.order.appendleft(key)
if __name__ == "__main__":
cache = LRUCache(2)
cache.put(1, 1)
cache.put(2, 2)
print(cache.get(1))
cache.put(3, 3)
print(cache.get(2))
cache.put(4, 4)
print(cache.get(1))
print(cache.get(3))
print(cache.get(4))
C#
using System;
using System.Collections.Generic;
class LRUCache {
private int capacity;
private Dictionary<int, LinkedListNode<KeyValuePair<int, int>>> cacheMap;
private LinkedList<KeyValuePair<int, int>> lruList;
// Constructor to initialize the cache with a given capacity
public LRUCache(int capacity) {
this.capacity = capacity;
this.cacheMap = new Dictionary<int, LinkedListNode<KeyValuePair<int, int>>>();
this.lruList = new LinkedList<KeyValuePair<int, int>>();
}
// Function to get the value for a given key
public int Get(int key) {
if (cacheMap.TryGetValue(key, out LinkedListNode<KeyValuePair<int, int>> node)) {
// Move the accessed node to the front (most recently used position)
lruList.Remove(node);
lruList.AddFirst(node);
return node.Value.Value;
} else {
return -1;
}
}
// Function to put a key-value pair into the cache
public void Put(int key, int value) {
if (cacheMap.TryGetValue(key, out LinkedListNode<KeyValuePair<int, int>> node)) {
// Remove the old node from the list and map
lruList.Remove(node);
cacheMap.Remove(key);
}
// Insert the new node at the front of the list
var newNode = new KeyValuePair<int, int>(key, value);
var listNode = new LinkedListNode<KeyValuePair<int, int>>(newNode);
lruList.AddFirst(listNode);
cacheMap[key] = listNode;
// If the cache size exceeds the capacity, remove the
// least recently used item
if (cacheMap.Count > capacity) {
var lastNode = lruList.Last;
lruList.RemoveLast();
cacheMap.Remove(lastNode.Value.Key);
}
}
}
class GfG {
static void Main() {
LRUCache cache = new LRUCache(2);
cache.Put(1, 1);
cache.Put(2, 2);
Console.WriteLine(cache.Get(1));
cache.Put(3, 3);
Console.WriteLine(cache.Get(2));
cache.Put(4, 4);
Console.WriteLine(cache.Get(1));
Console.WriteLine(cache.Get(3));
Console.WriteLine(cache.Get(4));
}
}
JavaScript
// Javascript program to implement LRU Least Recently Used)
// using Built-in Doubly linked list
class LRUCache {
constructor(capacity) {
this.capacity = capacity;
this.cache = new Map();
}
// Get the value for a given key
get(key) {
if (!this.cache.has(key)) {
return -1;
}
// Move the accessed key-value pair
// to the end to mark it as recently used
const value = this.cache.get(key);
this.cache.delete(key);
this.cache.set(key, value);
return value;
}
// Put a key-value pair into the cache
put(key, value) {
if (this.cache.has(key)) {
// Update the value and move the key to the end
this.cache.delete(key);
}
else if (this.cache.size >= this.capacity) {
// Remove the least recently used item (the
// first item in the Map)
this.cache.delete(this.cache.keys().next().value);
}
// Add the new key-value pair
this.cache.set(key, value);
}
}
const cache = new LRUCache(2);
cache.put(1, 1);
cache.put(2, 2);
console.log(cache.get(1));
cache.put(3, 3);
console.log(cache.get(2));
cache.put(4, 4);
console.log(cache.get(1));
console.log(cache.get(3));
console.log(cache.get(4));
Time Complexity : get(key) – O(1) and put(key, value) – O(1)
Auxiliary Space: O(capacity)