1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package edu.caltech.cs2.datastructures;
import edu.caltech.cs2.interfaces.ICollection;
import edu.caltech.cs2.interfaces.IDictionary;
import java.util.Iterator;
public class MoveToFrontDictionary<K, V> implements IDictionary<K,V> {
//keep track global head <Node <K, V>
//dictionary nodes have K key, V value
//Node<K,V> next
private Node<K, V> head;
private int size;
public MoveToFrontDictionary() {
this.head = null;
this.size = 0;
}
private static class Node<K, V> {
public K key;
public V value;
public Node<K, V> next;
public Node(K key, V value) {
this(key, value, null);
}
public Node(K key, V value, Node<K, V> next){
this.key = key;
this.value = value;
this.next = next;
}
}
@Override
public V remove(K key) {
//call get() on key
//head = head.next
if (this.head == null){
return null;
}
if (!this.containsKey(key)){
return null;
}
if (this.head.key.equals(key)){
V value = this.head.value;
this.head = this.head.next;
this.size --;
return value;
}
Node<K, V> current = this.head;
Node<K, V> prev = null;
while (current != null && !current.key.equals(key)){
prev = current;
current = current.next;
}
if (current == null) {
return null;
}
prev.next = current.next;
this.size--;
return current.value;
}
@Override
public V put(K key, V value) {
//save head node as oldHead
//Head = new Node(key, val)
//head.next = old.head (???)
V oldHead = this.remove(key);
Node<K, V> node = new Node<K, V>(key, value);
if (this.head != null){
node.next = this.head;
}
this.head = node;
this.size++;
return oldHead;
}
@Override
public boolean containsKey(K key) {
return this.keys().contains(key);
}
@Override
public boolean containsValue(V value) {
return this.values().contains(value);
}
@Override
public int size() {
return this.size;
}
@Override
public ICollection<K> keys() {
ICollection<K> deq = new ArrayDeque<>();
if (this.head == null) {
return deq;
}
Node<K, V> current = this.head;
while (current != null) {
deq.add(current.key);
current = current.next;
}
return deq;
}
@Override
public ICollection<V> values() {
ICollection<V> deq = new ArrayDeque<>();
if (this.head == null){
return deq;
}
Node<K, V> current = this.head;
while (current != null) {
deq.add(current.value);
current = current.next;
}
return deq;
}
public V get(K key) {
if (this.head == key){
return this.head.value;
}
else {
Node<K, V> curr = this.head;
while (curr != null) {
if (curr.key.equals(key)){
V val = curr.value;
/*
//rather than use put, directly change the pointers of the node being accessed
Node<K, V> nxtNode = curr.next;
nxtNode.next = this.head;
this.head = nxtNode;
//previous node
//create a new node at front of list with the node that is being accessed
*/
this.put(key, val);
return val;
}
curr = curr.next;
}
}
return null;
}
@Override
public Iterator<K> iterator() {
return new MoveToFrontIterator(this);
}
private class MoveToFrontIterator implements Iterator<K>{
//take linkeddeque iterator
private Node<K, V> currNode = null;
public MoveToFrontIterator(MoveToFrontDictionary dict){
this.currNode = dict.head;
}
@Override
public boolean hasNext() {
return this.currNode != null;
}
@Override
public K next() {
K element = currNode.key;
this.currNode = currNode.next;
return element;
}
}
}