LRU算法实现

LRU(Least Recently Used),最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰。该算法面试常备,本文以JAVA初级实现。
LRU详情参考 百度Wiki

import java.util.HashMap;
import java.util.Map;

class LRUNode {
    String key;
    Object value;
    LRUNode next;
    LRUNode pre;

    public LRUNode(String key, Object value) {
        this.key = key;
        this.value = value;
    }
}

public class LRU {
    //Initialize var
    Map<String, LRUNode> map = new HashMap<>();
    LRUNode head; //头指针
    LRUNode tail; //尾指针
    int capacity;

    public LRU(int capacity) {
        this.capacity = capacity;
    }
	//如果密钥不存在,则写入其数据值。当缓存容量达到上限时
	//它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间
    public void put(String key, Object value) {  
        if (head == null) { // 如果为空,进行初始化
            head = new LRUNode(key, value);
            tail = head;
            map.put(key, head);
        }
        LRUNode node = map.get(key);
        if (node != null) { // 更新value,并将node移动到head
            node.value = value;
            removeAndInsert(node);
        } else {
            LRUNode tmp = new LRUNode(key, value);
            if (map.size() >= capacity) { //删除尾节点
                map.remove(tail);
                tail = tail.pre;
                tail.next = null;
            }
            map.put(key, tmp);
            tmp.next = head;
            head.pre = tmp;
            head = tmp;
        }
    }
    
	// 获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数)
    public Object get(String key) { 
        LRUNode node = map.get(key);
        if (node != null) {
            removeAndInsert(node);
            return node.value;
        }
        return null;
    }

    private void removeAndInsert(LRUNode node) {  //删除节点,并插入头部
        if (node == head) return; //如果原本为头指针,不需要操作
        else if (node == tail) { //如果为尾指针,需把preNode.next设为null,tail标记为preNode
            tail = node.pre;
            tail.next = null;
        } else {  //中间指针将前指针.next设为当前指针的.next;同理后指针.pre
            node.pre.next = node.next;
            node.next.pre = node.pre;
        }
        // 将node指针插入头结点
        node.next = head;
        node.pre = null;
        head.pre = node;
        head = node;
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是LRU算法的C++实现: ```c++ #include <iostream> #include <list> #include <unordered_map> using namespace std; class LRUCache { public: LRUCache(int capacity) { cap = capacity; } int get(int key) { auto it = cache.find(key); if (it == cache.end()) { return -1; } // 将当前访问的节点移到链表头部,并更新哈希表中该节点的地址 cacheList.splice(cacheList.begin(), cacheList, it->second); it->second = cacheList.begin(); return it->second->second; } void put(int key, int value) { auto it = cache.find(key); if (it != cache.end()) { // 如果 key 已经存在,先删除旧的节点 cacheList.erase(it->second); } // 插入新节点到链表头部,并在哈希表中添加该节点 cacheList.push_front(make_pair(key, value)); cache[key] = cacheList.begin(); // 如果超出缓存容量,删除链表尾部节点,并在哈希表中删除对应的项 if (cache.size() > cap) { int k = cacheList.rbegin()->first; cacheList.pop_back(); cache.erase(k); } } private: int cap; list<pair<int, int>> cacheList; unordered_map<int, list<pair<int, int>>::iterator> cache; }; int main() { LRUCache cache(2); cache.put(1, 1); cache.put(2, 2); cout << cache.get(1) << endl; // 输出 1 cache.put(3, 3); cout << cache.get(2) << endl; // 输出 -1,因为缓存中已经删除了 key 为 2 的项 cache.put(4, 4); cout << cache.get(1) << endl; // 输出 -1,因为缓存中已经删除了 key 为 1 的项 cout << cache.get(3) << endl; // 输出 3 cout << cache.get(4) << endl; // 输出 4 return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值