1
+ #include < iostream>
2
+ #include < unordered_map>
3
+ using namespace std ;
4
+
5
+ class DoublyLinkedListNode {
6
+ public:
7
+ string key;
8
+ int value;
9
+ DoublyLinkedListNode* next;
10
+ DoublyLinkedListNode* prev;
11
+ DoublyLinkedListNode (string key, int value) {
12
+ this ->key = key;
13
+ this ->value = value;
14
+ this ->prev = NULL ;
15
+ this ->next = NULL ;
16
+ }
17
+ void removeBindings () {
18
+ if (this ->prev != NULL ) {
19
+ this ->prev ->next = this ->next ;
20
+ }
21
+ if (this ->next != NULL ) {
22
+ this ->next ->prev = this ->prev ;
23
+ }
24
+ this ->prev = NULL ;
25
+ this ->next = NULL ;
26
+ }
27
+ };
28
+ class DoublyLinkedList {
29
+ public:
30
+ DoublyLinkedListNode* head;
31
+ DoublyLinkedListNode* tail;
32
+
33
+ DoublyLinkedList () {
34
+ this ->head = NULL ;
35
+ this ->tail = NULL ;
36
+ }
37
+ void setHeadTo (DoublyLinkedListNode* node) {
38
+ if (head == node) {
39
+ return ;
40
+ } else if (this ->head == NULL ) {
41
+ this ->head = node;
42
+ this ->tail = node;
43
+ return ;
44
+ } else if (this ->head == this ->tail ) {
45
+ this ->head = node;
46
+ this ->head ->next = this ->tail ;
47
+ return ;
48
+ } else {
49
+ if (this ->tail == node) {
50
+ this ->removeTail ();
51
+ }
52
+ node->removeBindings ();
53
+ node->next = this ->head ;
54
+ this ->head ->prev = node;
55
+ this ->head = node;
56
+ }
57
+ }
58
+ void removeTail () {
59
+ if (this ->tail == NULL ) return ;
60
+ if (this ->tail == this ->head ) {
61
+ this ->tail = NULL ;
62
+ this ->head = NULL ;
63
+ return ;
64
+ }
65
+ this ->tail = this ->tail ->prev ;
66
+ this ->tail ->next = NULL ;
67
+ }
68
+ };
69
+ class LRUCache {
70
+ public:
71
+ int maxSize;
72
+ int currentCount;
73
+ DoublyLinkedList* listOfMostRecent;
74
+ unordered_map<string, DoublyLinkedListNode*> cache;
75
+
76
+ LRUCache (int maxSize) {
77
+ this ->maxSize = maxSize > 1 ? maxSize : 1 ;
78
+ this ->currentCount = 0 ;
79
+ this ->listOfMostRecent = new DoublyLinkedList ();
80
+ }
81
+ void insertKeyValuePair (string key, int val) {
82
+ // check if the key exists.
83
+ // if it exists then just update the value and the LL.
84
+ // if not present, check for the value of current number of keys
85
+ // if it is less than max size then just insert the value and update map
86
+ // else delete the least recently used value and insert into LL and update map
87
+ if (this ->cache .find (key) != this ->cache .end ()) {
88
+ this ->cache [key]->value = val;
89
+ this ->listOfMostRecent ->setHeadTo (this ->cache [key]);
90
+ } else {
91
+ DoublyLinkedListNode* node = new DoublyLinkedListNode (key, val);
92
+ if (this ->currentCount == this ->maxSize ) {
93
+ this ->listOfMostRecent ->removeTail ();
94
+ this ->currentCount --;
95
+ }
96
+ this ->listOfMostRecent ->setHeadTo (node);
97
+ this ->cache [key] = node;
98
+ this ->currentCount ++;
99
+ }
100
+ }
101
+
102
+ int * getValueFromKey (string key) {
103
+ // Write your code here.
104
+ if (this ->cache .find (key) != this ->cache .end ()) {
105
+ this ->listOfMostRecent ->setHeadTo (this ->cache [key]);
106
+ return &this ->cache [key]->value ;
107
+ }
108
+ return NULL ;
109
+ }
110
+
111
+ string getMostRecentKey () {
112
+ // Write your code here.
113
+ return this ->listOfMostRecent ->head ->key ;
114
+ }
115
+ };
116
+
117
+ int main () {
118
+ return 0 ;
119
+ }
0 commit comments