package com.algorithm.lru;

import java.util.HashMap;

/** * Simple LRU(least recently used) algorithm */
public class LRUCache<K.V> {

    private Node head, tail;

    private HashMap<K, Node> hashMap = new HashMap<>();

    private int size = 0;

    public int capacity = 10;

    public LRUCache(a) {}public LRUCache(int capacity) {
        this.capacity = capacity;
    }

    public void put(K key, V value) {
        Node node = hashMap.get(key);
        if(node ! =null) {
             node.value = value;
             if(node ! = head) {if(node.next ! =null) {
                    node.prev.next = node.next;
                    node.next.prev = node.prev;
                 }else{
                     tail = node.prev;
                     tail.next = null;
                 }
                 node.prev = null; node.next = head; head.prev = node; head = node; }}else{
            Node node1 = new Node();
            if (head == null && tail == null){
                head = tail = node1;
                head.key = key;
                head.value = value;
            }else{
                node.key = key;
                node.value = value;
                node.next = head;
                head.prev = node1;
                head = node1;
            }
            if (size + 1 <= capacity) {
                size ++;
            }else{
                Node newtail = tail.prev;
                tail.prev.next = null;
                tail.prev = null; tail = newtail; }}}public V get(K key) {
        // Walk through the list to find the node
        Node result = hashMap.get(key);
        // Find the node and the node is not a head
        if(result ! =null&& result ! = head) {// Fetch the queried node and process the pointer before and after the node
            if(result.next ! =null) {
                result.next.prev = result.prev;
            } else {
                tail = tail.prev;
                tail.next = null;
            }
            result.prev.next = result.next;
            // Insert the removed node into the header
            result.next = head;
            head.prev = result;
            head = result;
            return result.value;
        }
        return null;
    }

    /** ** test */
    public void show(a) {
        Node p = head;
        while(p ! =null) {
            System.out.printf("<%s,%s>", p.key, p.value);
            p = p.next;
        }
        System.out.println();
    }

    public class Node { Node prev,next; K key; V value; }}Copy the code