java什么时候用链表,java - 手写LRU(使用链表,时间复杂度O(n))

最简单的LRU实现,底层存储采用链表结构,时间复杂度为O(n)

代码如下:

package com.jfp;

/**

* @author jiafupeng

* @desc

* @create 2021/3/12 15:58

* @update 2021/3/12 15:58

**/

public class Test {

public static void main(String[] args) {

LRUCache lruCache = new LRUCache(5);

lruCache.put(1,1);

lruCache.put(2,2);

lruCache.put(3,3);

lruCache.put(4,4);

lruCache.put(5,5);

lruCache.put(6,6);

System.out.println(lruCache);

System.out.println(lruCache.get(1));

System.out.println(lruCache.get(4));

System.out.println(lruCache);

}

public static class LRUCache{

/**

* 长度

*/

private int size;

/**

* 最大容量

*/

private int capacity;

/**

* 链表头

*/

private NodeList headNodeList;

/**

* 链表尾

*/

private NodeList tailNodeList;

public LRUCache(int capacity) {

this.capacity = capacity;

this.size = 0;

}

private static class NodeList{

private Integer key;

private Integer value;

private NodeList pre;

private NodeList next;

public NodeList(Integer key, Integer value, NodeList pre, NodeList next) {

this.key = key;

this.value = value;

this.pre = pre;

this.next = next;

}

}

public void put(int key, int val){

NodeList getNodeList = getNodeList(key);

if(getNodeList != null){

getNodeList.value = val;

return;

}

if(headNodeList == null){

init(key, val);

} else {

tailNodeList.next = new NodeList(key, val, tailNodeList, null);

tailNodeList = tailNodeList.next;

size++;

}

if(size > capacity){

removeHead();

}

}

public Integer get(int key){

NodeList getNodeList = getNodeList(key);

return getNodeList == null ? null : getNodeList.value;

}

@Override

public String toString(){

NodeList nodeList = headNodeList;

String str = "";

while (nodeList != null){

str += ("[" + nodeList.key + ":" + nodeList.value + "]=>");

nodeList = nodeList.next;

}

return str + "null";

}

private void removeHead(){

headNodeList = headNodeList.next;

}

private void init(int key, int value){

headNodeList = new NodeList(key, value, null,null);

tailNodeList = headNodeList;

size++;

}

private NodeList getNodeList(int key){

NodeList tmpNodeList = headNodeList;

while(tmpNodeList != null){

if(tmpNodeList.key == key){

// 换位置

moveToTail(tmpNodeList);

return tmpNodeList;

}

tmpNodeList = tmpNodeList.next;

}

return null;

}

private void moveToTail(NodeList nodeList){

if(nodeList == tailNodeList){

return;

}

if(nodeList == headNodeList){

headNodeList = nodeList.next;

}

NodeList pre = nodeList.pre;

NodeList next = nodeList.next;

if(pre != null){

pre.next = next;

}

if(next != null){

next.pre = pre;

}

tailNodeList.next = nodeList;

nodeList.pre = tailNodeList;

nodeList.next = null;

tailNodeList = nodeList;

}

}

}

输出

[2:2]=>[3:3]=>[4:4]=>[5:5]=>[6:6]=>null

null

4

[2:2]=>[3:3]=>[5:5]=>[6:6]=>[4:4]=>null

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值