Skip to content

Commit 266b6a1

Browse files
committed
Updated LRU solution
1 parent 7f76d59 commit 266b6a1

File tree

4 files changed

+48
-57
lines changed

4 files changed

+48
-57
lines changed
Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,22 @@
11
package _16_25_LRU_Cache;
22

33
public class DoublyLinkedList {
4-
Node head = null;
5-
Node tail = null;
4+
private Node head = null;
5+
private Node tail = null;
66

7-
void addFirst(Node n) {
7+
public void addFirst(Node n) {
88
if (head == null) {
99
head = n;
1010
tail = n;
1111
} else {
12+
n.prev = null;
1213
n.next = head;
1314
head.prev = n;
1415
head = n;
1516
}
1617
}
17-
18-
void removeLast() {
19-
removeNode(tail);
20-
// if (tail == null) {
21-
// return null;
22-
// }
23-
// if (head == tail) { // only 1 Node in List
24-
// Node n = tail;
25-
// head = null;
26-
// tail = null;
27-
// return n;
28-
// } else {
29-
// Node n = tail;
30-
// tail = n.prev;
31-
// tail.next = null;
32-
// n.prev = null;
33-
// return n;
34-
// }
35-
}
36-
37-
void removeNode(Node n) {
18+
19+
public void remove(Node n) { // Assumes "Node n" is in this list
3820
if (n == null) {
3921
return;
4022
}
@@ -51,4 +33,17 @@ void removeNode(Node n) {
5133
tail = n.prev;
5234
}
5335
}
36+
37+
public void updateFreshness(Node n) { // Assumes "Node n" is in this list
38+
remove(n);
39+
addFirst(n);
40+
}
41+
42+
public Node getHead() {
43+
return head;
44+
}
45+
46+
public Node getTail() {
47+
return tail;
48+
}
5449
}

Chp. 16 - More Problems (Moderate)/_16_25_LRU_Cache/LRUCache.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
// Answer: Because when we remove from end of Linked List, the Node's key is used
1212
// to find the Node in the HashMap (to remove the node there as well)
1313

14-
class LRUCache {
14+
public class LRUCache {
1515
private int maxSize;
16-
private Map<Integer, Node> map; // gives us constant access to Nodes
16+
private Map<Integer, Node> map; // gives us O(1)-time access to Nodes
1717
private DoublyLinkedList dll; // used to keep track of "freshness" of Nodes
1818

1919
public LRUCache(int maxSize) {
@@ -22,36 +22,34 @@ public LRUCache(int maxSize) {
2222
dll = new DoublyLinkedList();
2323
}
2424

25-
public void add(Integer key, Node value) {
26-
if (map.containsKey(key)) {
27-
get(key);
28-
} else {
29-
if (map.size() == maxSize) {
30-
Node n = dll.tail;
31-
dll.removeLast();
32-
map.remove(n.key);
33-
}
34-
map.put(key, value);
35-
dll.addFirst(value);
25+
public void add(int key, String value) {
26+
remove(key); // If key already exists, we will overwrite it.
27+
if (map.size() == maxSize) {
28+
remove(dll.getTail().key);
3629
}
30+
Node n = new Node(key, value);
31+
dll.addFirst(n);
32+
map.put(key, n);
3733
}
3834

39-
public Node get(Integer key) {
35+
public void remove(int key) {
4036
Node n = map.get(key);
37+
dll.remove(n);
38+
map.remove(key);
39+
}
4140

42-
// Update Freshness
43-
if (n.prev != null) {
44-
n.prev.next = n.next;
45-
}
46-
if (n.next != null) {
47-
n.next.prev = n.prev;
48-
}
49-
dll.addFirst(n);
50-
51-
return n;
41+
public String getValue(int key) {
42+
Node n = map.get(key);
43+
if (n == null) {
44+
return null;
45+
}
46+
if (n != dll.getHead()) {
47+
dll.updateFreshness(n);
48+
}
49+
return n.value;
5250
}
5351

54-
public DoublyLinkedList getItems() {
52+
public DoublyLinkedList getItems() { // added for testing
5553
return dll;
5654
}
5755
}

Chp. 16 - More Problems (Moderate)/_16_25_LRU_Cache/Node.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package _16_25_LRU_Cache;
22

33
public class Node {
4-
int key;
4+
int key;
55
String value;
66
Node next;
77
Node prev;
@@ -12,7 +12,7 @@ public Node(int k, String v) {
1212
next = null;
1313
prev = null;
1414
}
15-
15+
1616
@Override
1717
public String toString() {
1818
return value;

Chp. 16 - More Problems (Moderate)/_16_25_LRU_Cache/Tester.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ public static void main(String[] args) {
77
/* Create LRU Cache */
88
LRUCache cache = new LRUCache(7);
99
for (int i = 20; i >= 1; i--) {
10-
Node node = new Node(i, "Node " + i + " value");
11-
cache.add(i, node);
10+
cache.add(i, "Node " + i + " value");
1211
}
1312

1413
/* Access Item 4, and print cache items */
15-
cache.get(4); // should move it to head of cache
16-
DoublyLinkedList items = cache.getItems();
17-
Node head = items.head;
18-
Node n = head;
14+
cache.remove(1);
15+
cache.getValue(4); // should move it to head of cache
16+
Node n = cache.getItems().getHead();
1917
while (n != null) {
2018
System.out.println(n);
2119
n = n.next;

0 commit comments

Comments
 (0)