# LRU Cache implementation using Double Linked Lists

• Last Updated : 01 Sep, 2021

Given a pre define size of a list N and an array Arr. The task is to implement Least Recently Used(LRU) algorithm using Double Linked Lists.
The program takes two sets of inputs. First, The size of the linked list. Second, The element to search in the linked list.
Examples:

Input: N = 3, Arr = { 1, 2, 3 }
Output:
[0]->[0]->[0]->NULL
[1]->[0]->[0]->NULL
[2]->[1]->[0]->NULL
[3]->[2]->[1]->NULL
Input: N = 5, Arr = { 1, 2, 3, 4, 3, 8 }
Output:
[0]->[0]->[0]->[0]->[0]->NULL
[1]->[0]->[0]->[0]->[0]->NULL
[2]->[1]->[0]->[0]->[0]->NULL
[3]->[2]->[1]->[0]->[0]->NULL
[4]->[3]->[2]->[1]->[0]->NULL
[2]->[4]->[3]->[1]->[0]->NULL
[8]->[2]->[4]->[3]->[1]->NULL

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Approach:
The idea is very basic that keep inserting the elements at the head

• if the element is not present in the list the add it to the head of the list
• if the element is present in the list then move the element to the head and shift the remaining element of the list

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach` `#include ` `using` `namespace` `std;`   `// Creating the structure` `// for linkedlist` `struct` `doublelinkedlist {` `    ``int` `val;` `    ``struct` `doublelinkedlist* next;` `    ``struct` `doublelinkedlist* prev;` `};`   `// Creating three list for having` `// head, a temporarily list and` `// a list for tail` `struct` `doublelinkedlist* head;` `struct` `doublelinkedlist* tail;` `struct` `doublelinkedlist* temp;`   `int` `status;`   `// Function to add new node` `// in the list` `int` `AddNode(``int` `value)` `{` `    ``// if head is NULL creating` `    ``// the new node and assigning` `    ``// to head` `    ``if` `(head == NULL) {` `        ``head = (``struct` `doublelinkedlist*)` `            ``malloc``(``sizeof``(``struct` `doublelinkedlist));`   `        ``if` `(head == NULL) {` `            ``cout <<``"Unable to allocate space\n"``;` `            ``return` `-2;` `        ``}`   `        ``head->val = value;` `        ``tail = head;` `        ``head->prev = NULL;` `    ``}` `    ``else` `{`   `        ``temp = tail;` `        ``tail->next = (``struct` `doublelinkedlist*)` `            ``malloc``(``sizeof``(``struct` `doublelinkedlist));`   `        ``if` `(tail->next == NULL) {` `            ``cout <<``"Unable to allocate space\n"``;` `            ``return` `-2;` `        ``}`   `        ``tail->next->val = value;` `        ``tail = tail->next;` `        ``tail->prev = temp;` `    ``}` `    ``tail->next = NULL;`   `    ``return` `0;` `}`   `// Function to print` `// the linked list` `int` `Display(``void``)` `{` `    ``if` `(head == NULL) {` `        ``cout <<``"Add a node first\n"``;` `        ``return` `-2;` `    ``}` `    ``else` `{` `        ``temp = head;` `        ``while` `(temp != NULL) {` `            ``cout <<``"[ ]->"``<< temp->val;` `            ``temp = temp->next;` `        ``}` `        ``cout <<``"NULL\n"``;` `    ``}` `    ``return` `0;` `}`   `// Function to search the` `// elements is already present` `// in the list or not` `int` `SearchCache(``int` `value)` `{` `    ``if` `(head == NULL) {` `        ``cout <<``"Add a node first\n"``;` `        ``return` `-1;` `    ``}`   `    ``// Store head temporarily.` `    ``temp = head;`   `    ``// Traverse Double Linked List.` `    ``while` `(temp != NULL)`   `    ``{` `        ``// If value in list` `        ``// matches with given value.` `        ``if` `(temp->val == value)`   `        ``{` `            ``// Shift all values before` `            ``// the found value to the right.` `            ``while` `(temp != head) {` `                ``temp->val = temp->prev->val;` `                ``temp = temp->prev;` `            ``}`   `            ``// Place the found` `            ``// value at the head.` `            ``head->val = value;` `            ``return` `0;` `        ``}`   `        ``// Keep iterating the loop.` `        ``temp = temp->next;` `    ``}`   `    ``// For new elements.` `    ``temp = tail->prev;`   `    ``// Shift all value to the` `    ``// right and over-write` `    ``// the last value.` `    ``while` `(temp != NULL) {` `        ``temp->next->val = temp->val;` `        ``temp = temp->prev;` `    ``}`   `    ``// Place new value at head.` `    ``head->val = value;` `    ``return` `0;` `}`   `// Initializing function` `// that will create the` `// list with values 0 in it.` `int` `NumberOfNodes(``int` `number)` `{` `    ``static` `int` `i = 0;` `    ``for` `(i = 0; i < number; i += 1) {` `        ``status = AddNode(0);`   `        ``// if status is 0 then` `        ``// it will return` `        ``if` `(status < 0) {` `            ``cout <<``"Could not assign node\n"``;` `            ``return` `status;` `        ``}` `    ``}` `    ``return` `0;` `}`   `// This function will` `// remove the linked` `// list from the memory.` `int` `FreeCache(``int` `number)` `{` `    ``struct` `doublelinkedlist** freeing_ptr` `        ``= &head;` `    ``static` `int` `i = 0;`   `    ``for` `(i = 0; i < number; i += 1) {` `        ``free``(*freeing_ptr);` `        ``*freeing_ptr = NULL;` `        ``freeing_ptr += 1;` `    ``}` `    ``return` `0;` `}`   `// Function to perform LRU` `// operations` `void` `LRUOp(``int` `arr[], ``int` `n)` `{`   `    ``// Iterating through the` `    ``// elements so that LRU` `    ``// operation can take place` `    ``for` `(``int` `i = 0; i < n; ++i) {`   `        ``SearchCache(arr[i]);`   `        ``// If the status is -ve` `        ``// then return` `        ``if` `(status < 0) {` `            ``exit``(1);` `        ``}`   `        ``// Printing it every time` `        ``status = Display();` `    ``}` `}`   `// Driver Code` `int` `main(``void``)` `{` `    ``// Pre defining the` `    ``// size of the cache` `    ``int` `MEMSIZE = 5;` `    ``status = NumberOfNodes(MEMSIZE);`   `    ``// Number of elements` `    ``// to be added in LRU List.` `    ``int` `n = 10;`   `    ``// The Numbers to be` `    ``// added in LRU List.` `    ``int` `arr[] = { 1, 2, 3, 4, 5,` `                  ``2, 10, 7, 11, 1 };`   `    ``LRUOp(arr, n);`   `    ``// Removing the linked` `    ``// list from the memory.` `    ``FreeCache(MEMSIZE);` `    ``return` `0;` `}`   `// this code is contributed by shivanisinghss2110`

## C

 `// C implementation of the approach` `#include ` `#include ` `#include `   `// Creating the structure` `// for linkedlist` `struct` `doublelinkedlist {` `    ``int` `val;` `    ``struct` `doublelinkedlist* next;` `    ``struct` `doublelinkedlist* prev;` `};`   `// Creating three list for having` `// head, a temporarily list and` `// a list for tail` `struct` `doublelinkedlist* head;` `struct` `doublelinkedlist* tail;` `struct` `doublelinkedlist* temp;`   `int` `status;`   `// Function to add new node` `// in the list` `int` `AddNode(``int` `value)` `{` `    ``// if head is NULL creating` `    ``// the new node and assigning` `    ``// to head` `    ``if` `(head == NULL) {` `        ``head = (``struct` `doublelinkedlist*)` `            ``malloc``(``sizeof``(``struct` `doublelinkedlist));`   `        ``if` `(head == NULL) {` `            ``printf``(``"Unable to allocate space\n"``);` `            ``return` `-2;` `        ``}`   `        ``head->val = value;` `        ``tail = head;` `        ``head->prev = NULL;` `    ``}` `    ``else` `{`   `        ``temp = tail;` `        ``tail->next = (``struct` `doublelinkedlist*)` `            ``malloc``(``sizeof``(``struct` `doublelinkedlist));`   `        ``if` `(tail->next == NULL) {` `            ``printf``(``"Unable to allocate space\n"``);` `            ``return` `-2;` `        ``}`   `        ``tail->next->val = value;` `        ``tail = tail->next;` `        ``tail->prev = temp;` `    ``}` `    ``tail->next = NULL;`   `    ``return` `0;` `}`   `// Function to print` `// the linked list` `int` `Display(``void``)` `{` `    ``if` `(head == NULL) {` `        ``printf``(``"Add a node first\n"``);` `        ``return` `-2;` `    ``}` `    ``else` `{` `        ``temp = head;` `        ``while` `(temp != NULL) {` `            ``printf``(``"[%d]->"``, temp->val);` `            ``temp = temp->next;` `        ``}` `        ``printf``(``"NULL\n"``);` `    ``}` `    ``return` `0;` `}`   `// Function to search the` `// elements is already present` `// in the list or not` `int` `SearchCache(``int` `value)` `{` `    ``if` `(head == NULL) {` `        ``printf``(``"Add a node first\n"``);` `        ``return` `-1;` `    ``}`   `    ``// Store head temporarily.` `    ``temp = head;`   `    ``// Traverse Double Linked List.` `    ``while` `(temp != NULL)`   `    ``{` `        ``// If value in list` `        ``// matches with given value.` `        ``if` `(temp->val == value)`   `        ``{` `            ``// Shift all values before` `            ``// the found value to the right.` `            ``while` `(temp != head) {` `                ``temp->val = temp->prev->val;` `                ``temp = temp->prev;` `            ``}`   `            ``// Place the found` `            ``// value at the head.` `            ``head->val = value;` `            ``return` `0;` `        ``}`   `        ``// Keep iterating the loop.` `        ``temp = temp->next;` `    ``}`   `    ``// For new elements.` `    ``temp = tail->prev;`   `    ``// Shift all value to the` `    ``// right and over-write` `    ``// the last value.` `    ``while` `(temp != NULL) {` `        ``temp->next->val = temp->val;` `        ``temp = temp->prev;` `    ``}`   `    ``// Place new value at head.` `    ``head->val = value;` `    ``return` `0;` `}`   `// Initializing function` `// that will create the` `// list with values 0 in it.` `int` `NumberOfNodes(``int` `number)` `{` `    ``static` `int` `i = 0;` `    ``for` `(i = 0; i < number; i += 1) {` `        ``status = AddNode(0);`   `        ``// if status is 0 then` `        ``// it will return` `        ``if` `(status < 0) {` `            ``printf``(``"Could not assign node\n"``);` `            ``return` `status;` `        ``}` `    ``}` `    ``return` `0;` `}`   `// This function will` `// remove the linked` `// list from the memory.` `int` `FreeCache(``int` `number)` `{` `    ``struct` `doublelinkedlist** freeing_ptr` `        ``= &head;` `    ``static` `int` `i = 0;`   `    ``for` `(i = 0; i < number; i += 1) {` `        ``free``(*freeing_ptr);` `        ``*freeing_ptr = NULL;` `        ``freeing_ptr += 1;` `    ``}` `    ``return` `0;` `}`   `// Function to perform LRU` `// operations` `void` `LRUOp(``int` `arr[], ``int` `n)` `{`   `    ``// Iterating through the` `    ``// elements so that LRU` `    ``// operation can take place` `    ``for` `(``int` `i = 0; i < n; ++i) {`   `        ``SearchCache(arr[i]);`   `        ``// If the status is -ve` `        ``// then return` `        ``if` `(status < 0) {` `            ``exit``(1);` `        ``}`   `        ``// Printing it every time` `        ``status = Display();` `    ``}` `}`   `// Driver Code` `int` `main(``void``)` `{` `    ``// Pre defining the` `    ``// size of the cache` `    ``int` `MEMSIZE = 5;` `    ``status = NumberOfNodes(MEMSIZE);`   `    ``// Number of elements` `    ``// to be added in LRU List.` `    ``int` `n = 10;`   `    ``// The Numbers to be` `    ``// added in LRU List.` `    ``int` `arr[] = { 1, 2, 3, 4, 5,` `                  ``2, 10, 7, 11, 1 };`   `    ``LRUOp(arr, n);`   `    ``// Removing the linked` `    ``// list from the memory.` `    ``FreeCache(MEMSIZE);` `    ``return` `0;` `}`

Output:

```[1]->[0]->[0]->[0]->[0]->NULL
[2]->[1]->[0]->[0]->[0]->NULL
[3]->[2]->[1]->[0]->[0]->NULL
[4]->[3]->[2]->[1]->[0]->NULL
[5]->[4]->[3]->[2]->[1]->NULL
[2]->[5]->[4]->[3]->[1]->NULL
[10]->[2]->[5]->[4]->[3]->NULL
[7]->[10]->[2]->[5]->[4]->NULL
[11]->[7]->[10]->[2]->[5]->NULL
[1]->[11]->[7]->[10]->[2]->NULL```

My Personal Notes arrow_drop_up
Recommended Articles
Page :