手写LRU,以及自己实现一个简易的双向链表

class LRUCache {
  //采用链表+hashmap的方式来实现  
    DobbleLinkedList linkedList;
    HashMap<Integer, Node<Integer>> keyNodeMap;
   //反向存key  value 为了后面删除元素的时候方便通过node找到key,以便于keyNodeMap中删除key
    HashMap<Node<Integer>, Integer> nodeKeyMap;
    int capacity;
    public LRUCache(int capacity) {
        if(capacity <= 0)
            throw new RuntimeException(" capacity should be more than zero !");
        this.capacity = capacity;
        this.linkedList = new DobbleLinkedList();
        this.keyNodeMap = new HashMap<>();
        this.nodeKeyMap = new HashMap<>();
    }
    
    public int get(int key) {
        if(this.keyNodeMap.containsKey(key)){
            Node<Integer> node = this.keyNodeMap.get(key);
            linkedList.moveNodeToTail(node);
            return node.v;
        }
        return -1;
    }
    
    public void put(int key, int value) {
        Node<Integer> node;
        if(this.keyNodeMap.containsKey(key)){
             node = this.keyNodeMap.get(key);
            node.v = value;
        } else {
            node = new Node(value);
            this.keyNodeMap.put(key,node);
            this.nodeKeyMap.put(node,key);
            this.linkedList.addToTail(node);
        }
        this.linkedList.moveNodeToTail(node);

     if(this.capacity + 1 == this.keyNodeMap.size()){
            node = this.linkedList.removeFromHead();
            Integer k = this.nodeKeyMap.get(node);
            this.nodeKeyMap.remove(node);
            this.keyNodeMap.remove(k);
        }
    }

}

class Node<V>{
     Node pre;
     Node next;
     V v;
    Node(){}
    Node(V v){
        this.v = v;
    }
}

class DobbleLinkedList{
     Node head;
     Node tail;
    public Node addToTail(Node newNode){
        if(this.head == null){
            this.head = newNode;
            this.tail = newNode;
        } else{
            this.tail.next = newNode;
            newNode.pre =this.tail;
            this.tail = newNode;
        }
        return this.head;
    }

    public Node removeFromHead(){
        if(this.head == null)
            return null;
        Node oldHead = this.head;
        Node newHead = this.head.next;
        if(newHead == null){
            this.head = null;
        } else {
            newHead.pre = null;
            this.head.next = null;
            this.head = newHead;
        }
   
        return oldHead;
    }
  //将使用过的元素移动到双向链表的最末尾,因为删除元素的时候会删除头节点
    public void moveNodeToTail(Node node){
        if(node == null || node == this.tail) return;
        if(node == this.head){
            this.head = this.head.next;
            this.head.pre = null;
            node.next = null;
        } else {
            Node pre = node.pre;
            pre.next = node.next;
            node.next.pre = pre;
            node.next = null;
        }
        this.tail.next = node;
        node.pre = this.tail;
        this.tail = node;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值