栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现一个LRU算法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现一个LRU算法

一、核心思想

通过链表实现:

  • LRU算法思路:每次新增节点时
  • 1.去链表里面遍历,如果找到了,删掉后插入到头部,头部就是最新的链表。
  • 2.如果不在原来的链表里,如果有空间就插入头部,如果没有空间就删除最后一个。
二、代码
public class MyLRU {

    private LruNode head;
    private LruNode tail;


    
    private int size = 0;

    private static final int SIZE_MAX = 10;

    
    public void insertHead(int data) {
        LruNode newNode = new LruNode(data);
        if (head == null){
            tail = newNode;
        } else {
            head.pre = newNode;
            newNode.next = head;
        }
        head = newNode;
        size++;
    }

    
    public void deleteHead() {
        head = head.next;
        if (head != null) {
            head.pre = null;
        }
        size--;
    }

    
    public void deleteTail() {
        LruNode cur = head;
        while (cur.next != tail) {
            cur = cur.next;
        }
        tail = cur;
        tail.next = null;
        size--;
    }

    
    public void deleteTail(LruNode current) {
        // 移动tail标记
        tail = current.pre;
        tail.next = null;
        size--;
    }

    
    public void deleteKey(int data) {
        LruNode current = head;
        while (current != null) {
            if (current.value == data) {
                break;
            }
            current = current.next;
        }
        // 如果删除的是头部
        if (current == head) {
            deleteHead();
        } else {
            current.pre.next = current.next;
            // 删除的是尾部
            if (current == tail) {
                deleteTail(current);
            } else {
                current.next.pre = current.pre;
            }
        }
        size--;
    }

    
    public LruNode find(int data) {
        LruNode cur = head;
        while (cur != null) {
            if (cur.value == data) {
                break;
            }
            cur = cur.next;
        }
        return cur;
    }

    
    public void lruAddNode(int data) {
        // 遍历链表
        LruNode lruNode = find(data);
        // 如果新增的节点数据在原链表中
        if (null != lruNode) {
            // 删掉后插入到头部, 头部就是最新的链表。
            deleteKey(data);
            insertHead(data);
        } else {
            // 如果不在原来的链表里,如果有空间就插入头部,如果没有空间就删除最后一个
            if (size < SIZE_MAX) {
                insertHead(data);
            } else {
                deleteTail();
                insertHead(data);
            }
        }

    }

    
    public void print() {
        LruNode cur = head;
        while (cur != null) {
            System.out.println("cur: " + cur.value);
            cur = cur.next;
        }
    }

    
    public void getHead() {
        System.out.println("head = " + head.value);
    }

    
    public void getTail() {
        System.out.println("tail = " + tail.value);
    }


    public static void main(String[] args) {
        MyLRU myLRU = new MyLRU();
        for (int i = 10; i > 0; i--) {
            myLRU.lruAddNode(i);
        }
        myLRU.print();
        System.out.println("head: " + myLRU.head.value);
        System.out.println("tail: " + myLRU.tail.value);
        System.out.println("size: " + myLRU.size);
        myLRU.lruAddNode(8);
        myLRU.print();
        System.out.println("head: " + myLRU.head.value);
        System.out.println("tail: " + myLRU.tail.value);
        System.out.println("size: " + myLRU.size);
        myLRU.lruAddNode(11);
        myLRU.print();
        System.out.println("head: " + myLRU.head.value);
        System.out.println("tail: " + myLRU.tail.value);
        System.out.println("size: " + myLRU.size);
    }
}


class LruNode {

    
    int value;

    
    LruNode next;

    
    LruNode pre;

    LruNode(int value) {
        this.value = value;
        this.next = null;
        this.pre = null;
    }
}
三、答疑

不明之处可以评论留言或者联系博主解答

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/445331.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号