//Returns the value of the specified key in O(1) time.. public int Get(int key) { if (dict.ContainsKey(key)) { DoubleLinkedListNode node = dict[key]; RemoveNode(node); InsertNode(node); return(node.val); } return(-1); }
//Adds or updates the key in the cache in O(1) time. public void Put(int key, int value) { if (dict.ContainsKey(key)) { RemoveNode(dict[key]); } DoubleLinkedListNode nodeToAdd = new DoubleLinkedListNode(key, value); if (dict.Count == _capacity) { //If capacity is full, remove the node just before the dummy last node. RemoveNode(dummyLastNode.prev); } InsertNode(nodeToAdd); }