public int Get(int key) { if (!NodeDict.ContainsKey(key)) { return(-1); } int value = NodeDict[key].Value; this.RemoveNode(NodeDict[key]); this.AddToHead(NodeDict[key]); return(value); }
public void Put(int key, int value) { if (NodeDict.ContainsKey(key)) { NodeDict[key].Value = value; this.RemoveNode(NodeDict[key]); } else { if (NodeDict.Count == this.Capacity) { NodeDict.Remove(this.Tail.Key); this.RemoveNode(this.Tail); } CacheNode node = new CacheNode(key, value); NodeDict.Add(key, node); } this.AddToHead(NodeDict[key]); }