From ebbaab829a9b6a2384f9fddca99d9afec787e720 Mon Sep 17 00:00:00 2001 From: Hoda Date: Mon, 17 Dec 2018 22:53:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9F=BA=E4=BA=8E=E5=8D=95?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=AE=9E=E7=8E=B0LRU=E7=AE=97=E6=B3=95?= =?UTF-8?q?=EF=BC=88java=E7=89=88=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/06_linkedlist/LRUBaseLinkedList.java | 176 ++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 java/06_linkedlist/LRUBaseLinkedList.java diff --git a/java/06_linkedlist/LRUBaseLinkedList.java b/java/06_linkedlist/LRUBaseLinkedList.java new file mode 100644 index 0000000..57d94f3 --- /dev/null +++ b/java/06_linkedlist/LRUBaseLinkedList.java @@ -0,0 +1,176 @@ +package linked.singlelist; + + +import java.util.Scanner; + +/** + * 基于单链表LRU算法(java) + * + * @author hoda + * @create 2018-12-17 + */ +public class LRUBaseLinkedList { + + /** + * 默认链表容量 + */ + private final static Integer DEFAULT_CAPACITY = 10; + + /** + * 头结点 + */ + private SNode headNode; + + /** + * 链表长度 + */ + private Integer length; + + /** + * 链表容量 + */ + private Integer capacity; + + public LRUBaseLinkedList() { + this.headNode = new SNode<>(); + this.capacity = DEFAULT_CAPACITY; + this.length = 0; + } + + public LRUBaseLinkedList(Integer capacity) { + this.headNode = new SNode<>(); + this.capacity = capacity; + this.length = 0; + } + + public void add(T data) { + SNode preNode = findPreNode(data); + + // 链表中存在,删除原数据,再插入到链表的头部 + if (preNode != null) { + deleteElemOptim(preNode); + intsertElemAtBegin(data); + } else { + if (length >= this.capacity) { + //删除尾结点 + deleteElemAtEnd(); + } + intsertElemAtBegin(data); + } + } + + /** + * 删除preNode结点下一个元素 + * + * @param preNode + */ + private void deleteElemOptim(SNode preNode) { + SNode temp = preNode.getNext(); + preNode.setNext(temp.getNext()); + temp = null; + length--; + } + + /** + * 链表头部插入节点 + * + * @param data + */ + private void intsertElemAtBegin(T data) { + SNode next = headNode.getNext(); + headNode.setNext(new SNode(data, next)); + length++; + } + + /** + * 获取查找到元素的前一个结点 + * + * @param data + * @return + */ + private SNode findPreNode(T data) { + SNode node = headNode; + while (node.getNext() != null) { + if (data.equals(node.getNext().getElement())) { + return node; + } + node = node.getNext(); + } + return null; + } + + /** + * 删除尾结点 + */ + private void deleteElemAtEnd() { + SNode ptr = headNode; + // 空链表直接返回 + if (ptr.getNext() == null) { + return; + } + + // 倒数第二个结点 + while (ptr.getNext().getNext() != null) { + ptr = ptr.getNext(); + } + + SNode tmp = ptr.getNext(); + ptr.setNext(null); + tmp = null; + length--; + } + + private void printAll() { + SNode node = headNode.getNext(); + while (node != null) { + System.out.print(node.getElement() + ","); + node = node.getNext(); + } + System.out.println(); + } + + public class SNode { + + private T element; + + private SNode next; + + public SNode(T element) { + this.element = element; + } + + public SNode(T element, SNode next) { + this.element = element; + this.next = next; + } + + public SNode() { + this.next = null; + } + + public T getElement() { + return element; + } + + public void setElement(T element) { + this.element = element; + } + + public SNode getNext() { + return next; + } + + public void setNext(SNode next) { + this.next = next; + } + } + + public static void main(String[] args) { + LRUBaseLinkedList list = new LRUBaseLinkedList(); + Scanner sc = new Scanner(System.in); + while (true) { + list.add(sc.nextInt()); + list.printAll(); + } + } +}