单链表实现LRU Cache

package algo.linkedlist;

/**
 * Copyright (c) 2020 bigdata ALL Rights Reserved
 * Project: learning
 * Package: algo.linkedlist
 * Version: 1.0
 *
 * @author qingzhi.wu 2020/8/17 14:43
 */
public class LRUBaseLinkedList<E> {
    /**
     * 默认容量
     */
    private static final Integer DEFAULT_CAPACITTY = 10;
    /**
     * 默认长度
     */
    private Integer length;
    /**
     * 链表容量
     */
    private Integer capacity;
    /**
     * 头结点
     */
    private Node<E> head;

    public LRUBaseLinkedList(int capacity) {
        this.head = new Node<E>();
        this.capacity = capacity;
        this.length = 0;
    }

    public LRUBaseLinkedList() {
        this(DEFAULT_CAPACITTY);
    }

    public void add(E e) {
        Node preNode = findPreNode(e);
        if (preNode != null) {
            deleteElemOptim(preNode);
            insertElemAtBegin(e);
        } else {
            if (length >= this.capacity) {
                deleteElemAtEnd();
            }
            insertElemAtBegin(e);
        }
    }

    private void deleteElemAtEnd() {
        Node<E> ptr = head;
        if (ptr.getNext() == null) {
            return;
        }

        //找到倒数第二个结点
        while(ptr.getNext().getNext() != null){
            ptr = ptr.next;
        }

        Node temp = ptr.getNext();
        ptr.setNext(null);
        temp = null;
        length -- ;

    }

    private void insertElemAtBegin(E e) {
        Node<E> next = head.getNext();
        head.setNext(new Node<E>(next, e));
        length++;
    }

    /**
     * 删除这个结点的下一个结点元素
     *
     * @param preNode
     */
    private void deleteElemOptim(Node preNode) {
        Node temp = preNode.getNext();
        preNode.setE(temp.next);
        temp = null;
        length--;
    }

    private Node findPreNode(E e) {
        Node curNode = head;
        while (curNode.getNext() != null) {
            if (e.equals(curNode.getNext().getE())) {
                return curNode;
            }
            curNode = curNode.next;
        }
        return null;
    }
    private void printAll() {
        Node node = head.getNext();
        while (node != null) {
            System.out.print(node.getE() + ",");
            node = node.getNext();
        }
        System.out.println();
    }

    private static class Node<E> {
        private Node<E> next;
        private E e;

        public Node(Node<E> next, E e) {
            this.next = next;
            this.e = e;
        }

        public Node() {
        }

        public Node<E> getNext() {
            return next;
        }

        public void setNext(Node<E> next) {
            this.next = next;
        }

        public E getE() {
            return e;
        }

        public void setE(E e) {
            this.e = e;
        }
    }

    public static void main(String[] args) {
        LRUBaseLinkedList<Integer> lru_cache = new LRUBaseLinkedList<Integer>(10);
        for (int i = 0; i < 898; i++) {
            lru_cache.add(i);
        }
        lru_cache.printAll();
    }


}

你可能感兴趣的:(数据结构与算法)