From e906d5a6bd6461fd215f73aeb3963e804d6b06b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=B0=91=E6=B7=B3?= <1422034613@qq.com> Date: Fri, 8 Jan 2021 13:13:54 +0800 Subject: [PATCH] push --- .../DoublyLinkList.java" | 126 ++++++++++++++++++ "\344\275\234\344\270\232/HashTable.java" | 21 +++ "\344\275\234\344\270\232/LRU.java" | 42 ++++++ "\344\275\234\344\270\232/Node.java" | 24 ++++ "\344\275\234\344\270\232/Test.java" | 51 +++++++ 5 files changed, 264 insertions(+) create mode 100644 "\344\275\234\344\270\232/DoublyLinkList.java" create mode 100644 "\344\275\234\344\270\232/HashTable.java" create mode 100644 "\344\275\234\344\270\232/LRU.java" create mode 100644 "\344\275\234\344\270\232/Node.java" create mode 100644 "\344\275\234\344\270\232/Test.java" diff --git "a/\344\275\234\344\270\232/DoublyLinkList.java" "b/\344\275\234\344\270\232/DoublyLinkList.java" new file mode 100644 index 0000000..17c3bf7 --- /dev/null +++ "b/\344\275\234\344\270\232/DoublyLinkList.java" @@ -0,0 +1,126 @@ +public class DoublyLinkList { + private Node head; + + public boolean isExist(Object key) { + boolean flag = false; + Node head = this.head; + while (head != null){ + if (head.getKey().hashCode() == key.hashCode()) { + flag = true; + break; + } + head = head.getNext(); + } + return flag; + } + + public Object get(Object key) { + Node head = this.head; + while (head != null){ + if (head.getKey().hashCode() == key.hashCode()) { + return head.getValue(); + } + head = head.getNext(); + } + return null; + } + + public void insertPrev(Object key,Object value) { + if (this.head == null) { + this.head = new Node(key, value); + return; + } + Node node = new Node(key, value); + node.setNext(this.head); + this.head.setPrev(node); + this.head = node; + } + + public void insertNext(Object key,Object value) { + if (this.head == null) { + this.head = new Node(key, value); + return; + } + Node node = new Node(key, value); + Node head = this.head; + while (true){ + if (head.getNext()==null){ + node.setPrev(head); + head.setNext(node); + System.out.println("尾部添加:" + node.toString()); + return; + } + head = head.getNext(); + } + } + + public void toRear(Object key) { + Node head = this.head; + while (head != null) { + if (head.getKey().hashCode() == key.hashCode()) { + if (this.head.getNext() == null) { + return; + } + if (head.getPrev() != null) { + head.getPrev().setNext(head.getNext()); + } + if (head.getNext() != null) { + head.getNext().setPrev(head.getPrev()); + } + head.setPrev(null); + this.head.setPrev(head); + head.setNext(this.head); + this.head = head; + return; + } + else { + head = head.getNext(); + } + } + System.out.println("不存在指定元素"); + } + + public void deleteItem(Object key) { + deleteItem(key,this.head); + } + + public void print() { + print(this.head); + } + + private void deleteItem(Object key,Node node) { + if (node == null) { + return; + } + if (node.getKey().hashCode() == key.hashCode()) { + if (node.getPrev() == null&&node.getNext()==null) { + this.head = null; + return; + } + if (node.getPrev() == null) { + node.getNext().setPrev(null); + node = node.getNext(); + return; + } + if (node.getNext() == null) { + node.getPrev().setNext(null); + return; + } + node.getPrev().setNext(node.getNext()); + node.getNext().setPrev(node.getPrev()); + } + deleteItem(key,node.getNext()); + } + + private void print(Node node) { + if (node == null) { + System.out.println(""); + return; + } + System.out.print(node.getKey() + ":" + node.getValue() + " | "); + print(node.getNext()); + } + + public DoublyLinkList() { + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/HashTable.java" "b/\344\275\234\344\270\232/HashTable.java" new file mode 100644 index 0000000..4fa8ee9 --- /dev/null +++ "b/\344\275\234\344\270\232/HashTable.java" @@ -0,0 +1,21 @@ +public class LRU extends HashTable { + + private DoublyLinkList[] head; + + public LRU(int size) { + super(size); + } + + public Object get(Object key) { + int hashKey = key.hashCode(); + int index = hashKey % this.head.length; + this.head[index].toRear(key); + return this.head[index].get(key); + } + + public void set(Object key,Object value) { + int hashKey = key.hashCode(); + int index = hashKey % this.head.length; + this.head[index].insertPrev(key, value); + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/LRU.java" "b/\344\275\234\344\270\232/LRU.java" new file mode 100644 index 0000000..8038f29 --- /dev/null +++ "b/\344\275\234\344\270\232/LRU.java" @@ -0,0 +1,42 @@ +public class Node { + private Node next,prev; + private Object key; + private Object value; + + public Node getNext() { + return next; + } + + public void setNext(Node next) { + this.next = next; + } + + public Node getPrev() { + return prev; + } + + public void setPrev(Node prev) { + this.prev = prev; + } + + public Object getKey() { + return key; + } + + public void setKey(Object key) { + this.key = key; + } + + public Object getValue() { + return value; + } + + public void setValue(Object value) { + this.value = value; + } + + public Node(Object key, Object value) { + this.key = key; + this.value = value; + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/Node.java" "b/\344\275\234\344\270\232/Node.java" new file mode 100644 index 0000000..52ee6e0 --- /dev/null +++ "b/\344\275\234\344\270\232/Node.java" @@ -0,0 +1,24 @@ +public class Test { + public static void main(String[] args) { +// DoublyLinkList linkList = new DoublyLinkList(); +// linkList.insertNext(1,"abc"); +// linkList.insertNext(2,"ccc"); +// linkList.insertNext(3,"aaa"); +// linkList.print(); +// System.out.println("========================"); +// linkList.insertPrev(0,"object"); +// linkList.print(); +// System.out.println("========================"); +// linkList.deleteItem(1); +// linkList.print(); + HashTable hashTable = new HashTable(6); + hashTable.put(1,"sss1111"); + hashTable.put(2,"sss2222"); + hashTable.put(3,"sss3333"); + hashTable.put(4,"sss4444"); + hashTable.put(5,"sss5555"); + hashTable.put(11,"sss151515"); + hashTable.put(6,"sss6666"); + hashTable.getItems(); + } +} \ No newline at end of file diff --git "a/\344\275\234\344\270\232/Test.java" "b/\344\275\234\344\270\232/Test.java" new file mode 100644 index 0000000..c389fc6 --- /dev/null +++ "b/\344\275\234\344\270\232/Test.java" @@ -0,0 +1,51 @@ +public class HashTable { + + private DoublyLinkList[] head; + + public void getItems() { + for (int i = this.head.length - 1;i >= 0;i--){ + if (this.head[i] != null) { + this.head[i].print(); + } + } + } + + public boolean contains(Object key) { + int hashKey = key.hashCode(); + int index = hashKey % this.head.length; + if (this.head[index] == null) { + return false; + }else { + return this.head[index].isExist(hashKey); + } + } + + public void put(Object key,Object value) { + int hashKey = key.hashCode(); + int index = hashKey % this.head.length; + if (this.head[index] == null) { + this.head[index] = new DoublyLinkList(); + this.head[index].insertNext(hashKey,value); + return; + } + this.head[index].insertPrev(hashKey,value); + } + + public void deleteItem(Object key) { + int hashKey = key.hashCode(); + int index = hashKey % this.head.length; + if (this.head[index] == null) { + System.out.println("NO"); + return; + } + if (this.head[index].isExist(hashKey)) { + this.head[index].deleteItem(hashKey); + return; + } + System.out.println("NO"); + } + + public HashTable(int size) { + this.head = new DoublyLinkList[size]; + } +} \ No newline at end of file -- Gitee