From c72ced990f58ef60e4a1ad20e1238e23ff34ead5 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Sat, 25 Dec 2021 17:02:00 +0800 Subject: [PATCH] refactor --- .../linkedlist/{ListNode.java => Node.java} | 29 +++-- .../linkedlist/ReverseLinkedList.java | 48 +++----- .../queue/DynamicArrayQueue.java | 103 ++++++++++++++++++ .../datastructure/queue/LinkedListQueue.java | 93 ++++++++++++++++ .../datastructure/queue/LinkedListQueue2.java | 95 ++++++++++++++++ 5 files changed, 327 insertions(+), 41 deletions(-) rename budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/{ListNode.java => Node.java} (76%) create mode 100644 budd-common/src/main/java/io/github/ehlxr/datastructure/queue/DynamicArrayQueue.java create mode 100644 budd-common/src/main/java/io/github/ehlxr/datastructure/queue/LinkedListQueue.java create mode 100644 budd-common/src/main/java/io/github/ehlxr/datastructure/queue/LinkedListQueue2.java diff --git a/budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/ListNode.java b/budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/Node.java similarity index 76% rename from budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/ListNode.java rename to budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/Node.java index e8bf51e..4ee19ad 100644 --- a/budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/ListNode.java +++ b/budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/Node.java @@ -30,35 +30,42 @@ package io.github.ehlxr.datastructure.linkedlist; * @author ehlxr * @since 2021-12-23 14:41. */ -public class ListNode { - private int val; - private ListNode next; +public class Node { + private Integer val; + private Node next; - public ListNode(int val, ListNode next) { + public Node(Integer val, Node next) { this.val = val; this.next = next; } - public int getVal() { + public Integer getVal() { return val; } - public void setVal(int val) { + public void setVal(Integer val) { this.val = val; } - public ListNode getNext() { + public Node getNext() { return next; } - public void setNext(ListNode next) { + public void setNext(Node next) { this.next = next; } + public void print() { + Node n = this; + while (n != null) { + System.out.printf("%s -> ", n); + n = n.getNext(); + } + System.out.println(); + } + @Override public String toString() { - return "SingleNode{" + - "val=" + val + - '}'; + return "Node{" + val + '}'; } } diff --git a/budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/ReverseLinkedList.java b/budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/ReverseLinkedList.java index 248b12d..f2534aa 100644 --- a/budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/ReverseLinkedList.java +++ b/budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/ReverseLinkedList.java @@ -32,45 +32,33 @@ package io.github.ehlxr.datastructure.linkedlist; */ public class ReverseLinkedList { public static void main(String[] args) { - ListNode n5 = new ListNode(5, null); - ListNode n4 = new ListNode(4, n5); - ListNode n3 = new ListNode(3, n4); - ListNode n2 = new ListNode(2, n3); - ListNode n1 = new ListNode(1, n2); - ListNode head = new ListNode(0, n1); + Node n5 = new Node(5, null); + Node n4 = new Node(4, n5); + Node n3 = new Node(3, n4); + Node n2 = new Node(2, n3); + Node n1 = new Node(1, n2); + Node head = new Node(0, n1); - print(head); + head.print(); System.out.println("-------------"); // head = reverse(head); head = reverse2(head); // head = reverse3(head, null); - print(head); - + head.print(); } - public static void print(ListNode head) { - if (head == null) { - return; - } - - ListNode n = head; - while (n != null) { - System.out.println(n); - n = n.getNext(); - } - } // 循环方式 - public static ListNode reverse(ListNode head) { + public static Node reverse(Node head) { if (head == null || head.getNext() == null) { return head; } - ListNode pre = null; + Node pre = null; - ListNode cur = head; + Node cur = head; while (cur != null) { - ListNode next = cur.getNext(); + Node next = cur.getNext(); cur.setNext(pre); pre = cur; @@ -81,13 +69,13 @@ public class ReverseLinkedList { } // 递归一 - public static ListNode reverse2(ListNode head) { + public static Node reverse2(Node head) { if (head == null || head.getNext() == null) { return head; } - ListNode next = head.getNext(); - ListNode node = reverse2(next); + Node next = head.getNext(); + Node node = reverse2(next); head.getNext().setNext(head); head.setNext(null); @@ -96,13 +84,13 @@ public class ReverseLinkedList { } // 递归二 - public static ListNode reverse3(ListNode head, ListNode pre) { + public static Node reverse3(Node head, Node pre) { if (head == null) { return pre; } - ListNode next = head.getNext(); - ListNode node = reverse3(next, head); + Node next = head.getNext(); + Node node = reverse3(next, head); head.setNext(pre); diff --git a/budd-common/src/main/java/io/github/ehlxr/datastructure/queue/DynamicArrayQueue.java b/budd-common/src/main/java/io/github/ehlxr/datastructure/queue/DynamicArrayQueue.java new file mode 100644 index 0000000..3f91fc2 --- /dev/null +++ b/budd-common/src/main/java/io/github/ehlxr/datastructure/queue/DynamicArrayQueue.java @@ -0,0 +1,103 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2021 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.datastructure.queue; + +/** + * 数组实现动态扩容队列 + * + * @author ehlxr + * @since 2021-12-25 15:19. + */ +public class DynamicArrayQueue { + private final String[] data; + private final int size; + private int head = 0; + private int tail = 0; + + public DynamicArrayQueue(int size) { + data = new String[size]; + this.size = size; + } + + public static void main(String[] args) { + DynamicArrayQueue queue = new DynamicArrayQueue(5); + + // 队列容量为 5,入队 6 个最后一个失败 + for (int i = 0; i < 6; i++) { + System.out.println(queue.enqueue("" + i)); + } + + // 出队 2 个,然后还可以入队 2 个 + for (int i = 0; i < 2; i++) { + System.out.println(queue.dequeue()); + } + System.out.println(queue.enqueue("a")); + System.out.println(queue.enqueue("b")); + System.out.println(queue.enqueue("c")); + + // 出队 6 个,最后一个失败 + for (int i = 0; i < 6; i++) { + System.out.println(queue.dequeue()); + } + } + + /** + * 入队(从队尾添加元素) + */ + public boolean enqueue(String item) { + // 判断队列是否已满 + if (tail == size) { + if (head == 0) { + return false; + } + + // 如果 tail == size,而 head != 0,0 与 head 之间还有空位,向前移动 head 与 tail 之间的数据 + // for (int i = head; i < tail; i++) { + // data[i - head] = data[i]; + // } + if (tail - head >= 0) { + System.arraycopy(data, head, data, 0, tail - head); + } + + tail -= head; + head = 0; + } + + data[tail++] = item; + return true; + } + + /** + * 出队(从队首取出元素) + */ + public String dequeue() { + // 判断队列是否为空 + if (head == tail) { + return null; + } + + return data[head++]; + } +} diff --git a/budd-common/src/main/java/io/github/ehlxr/datastructure/queue/LinkedListQueue.java b/budd-common/src/main/java/io/github/ehlxr/datastructure/queue/LinkedListQueue.java new file mode 100644 index 0000000..89cc77f --- /dev/null +++ b/budd-common/src/main/java/io/github/ehlxr/datastructure/queue/LinkedListQueue.java @@ -0,0 +1,93 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2021 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.datastructure.queue; + +import io.github.ehlxr.datastructure.linkedlist.Node; + +/** + * 基于链表实现的队列 + *

+ * 入队时间复杂度 O(n) + * + * @author ehlxr + * @since 2021-12-25 16:02. + */ +public class LinkedListQueue { + private Node head; + + public static void main(String[] args) { + LinkedListQueue queue = new LinkedListQueue(); + for (int i = 0; i < 6; i++) { + System.out.println(queue.enqueue(i)); + queue.getData().print(); + } + + for (int i = 0; i < 6; i++) { + queue.getData().print(); + System.out.println(queue.dequeue()); + } + + System.out.println(queue.dequeue()); + } + + /** + * 入队(从队尾添加元素) + * 时间复杂度 O(n) + */ + public boolean enqueue(Integer item) { + Node node = new Node(item, null); + if (head == null) { + head = node; + return true; + } + + // 找到队尾 + Node tail = head; + while (tail.getNext() != null) { + tail = tail.getNext(); + } + + tail.setNext(node); + return true; + } + + /** + * 出队(从队首取出元素) + */ + public Integer dequeue() { + if (head == null) { + return null; + } + + Integer val = head.getVal(); + head = head.getNext(); + + return val; + } + + public Node getData() { + return head; + } +} diff --git a/budd-common/src/main/java/io/github/ehlxr/datastructure/queue/LinkedListQueue2.java b/budd-common/src/main/java/io/github/ehlxr/datastructure/queue/LinkedListQueue2.java new file mode 100644 index 0000000..cec0deb --- /dev/null +++ b/budd-common/src/main/java/io/github/ehlxr/datastructure/queue/LinkedListQueue2.java @@ -0,0 +1,95 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2021 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.datastructure.queue; + +import io.github.ehlxr.datastructure.linkedlist.Node; + +/** + * 基于链表实现的队列 + *

+ * 多一个变量 tail,换取入队时间复杂度为 O(1) + * + * @author ehlxr + * @since 2021-12-25 16:48. + */ +public class LinkedListQueue2 { + private Node head = null; + private Node tail = null; + + public static void main(String[] args) { + LinkedListQueue2 queue = new LinkedListQueue2(); + for (int i = 0; i < 6; i++) { + System.out.println(queue.enqueue(i)); + queue.getData().print(); + } + + for (int i = 0; i < 6; i++) { + queue.getData().print(); + System.out.println(queue.dequeue()); + } + + System.out.println(queue.dequeue()); + } + + /** + * 入队(从队尾添加元素) + * 时间复杂度 O(1) + */ + public boolean enqueue(Integer item) { + Node node = new Node(item, null); + + if (tail == null) { + head = node; + tail = node; + return true; + } + + tail.setNext(node); + tail = node; + return true; + } + + /** + * 出队(从队首取出元素) + */ + public Integer dequeue() { + if (head == null) { + return null; + } + + Integer val = head.getVal(); + head = head.getNext(); + // 如果队列为空了,需要把 tail 置空 + if (head == null) { + tail = null; + } + + return val; + } + + public Node getData() { + return head; + } +}