//remove a node from LinkedList private void removeNode(Node node){ Node curr = node; Node pre = curr.pre; Node post = curr.next; if (pre != null) pre.next = post; else head = post; if (post != null) post.pre = pre; else end = pre; }
//set a new node to the head of LinkedList private void setHead(Node node){ node.next = head; node.pre = null; if (head != null) head.pre = node; head = node; if (end == null) //if this node is the first of list end = node; }
//set key/value pair public void Set(int key, int value) { if (dic.ContainsKey (key)) { Node curr = dic [key]; curr.value = value; removeNode (curr); setHead (curr); } else { Node newNode = new Node (value,key); if (len == capacity) { Node last = end; removeNode (last); dic.Remove (last.key); dic.Add (key, newNode); setHead (newNode); } else { dic.Add (key, newNode); setHead (newNode); len++; } } }