首页 > 代码库 > LRU简单实现
LRU简单实现
<pre name="code" class="java">import java.util.HashMap; import java.util.Map; public class LRUCache { private int capacity; private int len; class Data { int key; int value; Data next; Data pre; } private Map<Integer, Data> dataSet; private Data head; private Data rail; public LRUCache(int capacity) { this.capacity = capacity; this.len = 0; this.head = null; this.rail = null; this.dataSet = new HashMap<Integer, Data>(); } public int get(int key) { if (!dataSet.containsKey(key)) return -1; Data temp = dataSet.get(key); if (temp == head) { return temp.value; } else if (temp == rail) { temp.pre.next = null; rail = temp.pre; } else { temp.pre.next = temp.next; temp.next.pre = temp.pre; } temp.next = head; temp.pre = null; head.pre = temp; head = temp; return temp.value; } public void set(int key, int value) { if (capacity == 0) return; if (dataSet.containsKey(key)) { Data temp = dataSet.get(key); if (temp == head) { temp.value = http://www.mamicode.com/value;>LRU简单实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。