public void AddNode(DoublyLinked node) { // Insert after head dummy node node.prev = head; node.next = head.next; head.next = node; node.next.prev = node; }
public void PopTail() { map.Remove(tail.prev.key); DoublyLinked prevTail = tail.prev.prev; prevTail.next = tail; tail.prev = prevTail; contains--; }
public LRUCache(int capacity) { this.map = new Dictionary <int, DoublyLinked>(); this.capacity = capacity; this.head = new DoublyLinked(0); this.tail = new DoublyLinked(0); this.head.next = this.tail; this.tail.prev = this.head; }
public void addNode(object data) { DoublyLinked newNode = new DoublyLinked(); newNode.Data = data; if (Head == null) { Head = Tail = newNode; Head.Prev = null; Tail.Next = null; } else { Tail.Next = newNode; newNode.Prev = Tail; Tail = newNode; Tail.Next = null; } }
public void Put(int key, int value) { if (map.ContainsKey(key)) { map[key].val = value; MoveToHead(map[key]); } else { var node = new DoublyLinked(value); node.key = key; contains++; map[key] = node; AddNode(node); if (contains > capacity) { PopTail(); } } }
public DoublyLinkedList() { Head = new DoublyLinked(); Tail = new DoublyLinked(); }
public void RemoveNode(DoublyLinked node) { // Remove node from position in list node.prev.next = node.next; node.next.prev = node.prev; }
public void MoveToHead(DoublyLinked node) { RemoveNode(node); AddNode(node); }