diff --git a/budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/Node.java b/budd-common/src/main/java/io/github/ehlxr/datastructure/Node.java similarity index 80% rename from budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/Node.java rename to budd-common/src/main/java/io/github/ehlxr/datastructure/Node.java index 4ee19ad..b0fc89f 100644 --- a/budd-common/src/main/java/io/github/ehlxr/datastructure/linkedlist/Node.java +++ b/budd-common/src/main/java/io/github/ehlxr/datastructure/Node.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package io.github.ehlxr.datastructure.linkedlist; +package io.github.ehlxr.datastructure; /** * 链表节点数据结构 @@ -30,37 +30,38 @@ package io.github.ehlxr.datastructure.linkedlist; * @author ehlxr * @since 2021-12-23 14:41. */ -public class Node { - private Integer val; - private Node next; +public class Node { + private K val; + private Node next; - public Node(Integer val, Node next) { + public Node(K val, Node next) { this.val = val; this.next = next; } - public Integer getVal() { + public K getVal() { return val; } - public void setVal(Integer val) { + public void setVal(K val) { this.val = val; } - public Node getNext() { + public Node getNext() { return next; } - public void setNext(Node next) { + public void setNext(Node next) { this.next = next; } public void print() { - Node n = this; + Node n = this; while (n != null) { - System.out.printf("%s -> ", n); + System.out.printf("%s -> ", n.getVal()); n = n.getNext(); } + System.out.print("null"); System.out.println(); } 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 f2534aa..4c7f8a3 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 @@ -24,6 +24,8 @@ package io.github.ehlxr.datastructure.linkedlist; +import io.github.ehlxr.datastructure.Node; + /** * 反转链表 * @@ -32,12 +34,12 @@ package io.github.ehlxr.datastructure.linkedlist; */ public class ReverseLinkedList { public static void main(String[] args) { - 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); + 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); head.print(); @@ -50,15 +52,15 @@ public class ReverseLinkedList { // 循环方式 - public static Node reverse(Node head) { + public static Node reverse(Node head) { if (head == null || head.getNext() == null) { return head; } - Node pre = null; + Node pre = null; - Node cur = head; + Node cur = head; while (cur != null) { - Node next = cur.getNext(); + Node next = cur.getNext(); cur.setNext(pre); pre = cur; @@ -69,13 +71,13 @@ public class ReverseLinkedList { } // 递归一 - public static Node reverse2(Node head) { + public static Node reverse2(Node head) { if (head == null || head.getNext() == null) { return head; } - Node next = head.getNext(); - Node node = reverse2(next); + Node next = head.getNext(); + Node node = reverse2(next); head.getNext().setNext(head); head.setNext(null); @@ -84,13 +86,13 @@ public class ReverseLinkedList { } // 递归二 - public static Node reverse3(Node head, Node pre) { + public static Node reverse3(Node head, Node pre) { if (head == null) { return pre; } - Node next = head.getNext(); - Node 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/LinkedListQueue.java b/budd-common/src/main/java/io/github/ehlxr/datastructure/queue/LinkedListQueue.java index 89cc77f..26e19fc 100644 --- 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 @@ -24,7 +24,7 @@ package io.github.ehlxr.datastructure.queue; -import io.github.ehlxr.datastructure.linkedlist.Node; +import io.github.ehlxr.datastructure.Node; /** * 基于链表实现的队列 @@ -35,13 +35,12 @@ import io.github.ehlxr.datastructure.linkedlist.Node; * @since 2021-12-25 16:02. */ public class LinkedListQueue { - private Node head; + 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++) { @@ -57,14 +56,14 @@ public class LinkedListQueue { * 时间复杂度 O(n) */ public boolean enqueue(Integer item) { - Node node = new Node(item, null); + Node node = new Node<>(item, null); if (head == null) { head = node; return true; } // 找到队尾 - Node tail = head; + Node tail = head; while (tail.getNext() != null) { tail = tail.getNext(); } @@ -87,7 +86,7 @@ public class LinkedListQueue { return val; } - public Node getData() { + 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 index cec0deb..2720612 100644 --- 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 @@ -24,7 +24,7 @@ package io.github.ehlxr.datastructure.queue; -import io.github.ehlxr.datastructure.linkedlist.Node; +import io.github.ehlxr.datastructure.Node; /** * 基于链表实现的队列 @@ -35,14 +35,13 @@ import io.github.ehlxr.datastructure.linkedlist.Node; * @since 2021-12-25 16:48. */ public class LinkedListQueue2 { - private Node head = null; - private Node tail = null; + 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++) { @@ -58,7 +57,7 @@ public class LinkedListQueue2 { * 时间复杂度 O(1) */ public boolean enqueue(Integer item) { - Node node = new Node(item, null); + Node node = new Node(item, null); if (tail == null) { head = node; @@ -89,7 +88,7 @@ public class LinkedListQueue2 { return val; } - public Node getData() { + public Node getData() { return head; } } diff --git a/budd-common/src/main/java/io/github/ehlxr/datastructure/stack/ArrayStack.java b/budd-common/src/main/java/io/github/ehlxr/datastructure/stack/ArrayStack.java new file mode 100644 index 0000000..caa68ff --- /dev/null +++ b/budd-common/src/main/java/io/github/ehlxr/datastructure/stack/ArrayStack.java @@ -0,0 +1,77 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2020 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.stack; + +/** + * @author ehlxr + * @since 2021-12-25 22:10. + */ +public class ArrayStack { + private final Integer[] data; + private final int size; + // 栈顶下标 + private int top = 0; + + public static void main(String[] args) { + ArrayStack stack = new ArrayStack(5); + for (int i = 0; i < 6; i++) { + System.out.println(stack.push(i)); + } + + for (int i = 0; i < 6; i++) { + System.out.println(stack.pop()); + } + + } + + public ArrayStack(int size) { + data = new Integer[size]; + this.size = size; + } + + /** + * 入栈 + */ + public boolean push(Integer item) { + // 判断栈是否已满 + if (top == size) { + return false; + } + data[top++] = item; + + return true; + } + + /** + * 出栈 + */ + public Integer pop() { + // 判断栈是否为空 + if (top == 0) { + return null; + } + return data[--top]; + } +} diff --git a/budd-common/src/main/java/io/github/ehlxr/datastructure/stack/LinkedListStack.java b/budd-common/src/main/java/io/github/ehlxr/datastructure/stack/LinkedListStack.java new file mode 100644 index 0000000..a714061 --- /dev/null +++ b/budd-common/src/main/java/io/github/ehlxr/datastructure/stack/LinkedListStack.java @@ -0,0 +1,80 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2020 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.stack; + +import io.github.ehlxr.datastructure.Node; + +/** + * @author ehlxr + * @since 2021-12-25 22:28. + */ +public class LinkedListStack { + private Node head; + + public Node getData() { + return head; + } + + public static void main(String[] args) { + LinkedListStack stack = new LinkedListStack(); + for (int i = 0; i < 6; i++) { + System.out.println(stack.push(i)); + } + + for (int i = 0; i < 6; i++) { + stack.getData().print(); + System.out.println(stack.pop()); + } + + } + + /** + * 入栈 + */ + public boolean push(Integer item) { + Node node = new Node<>(item, null); + if (head == null) { + head = node; + return true; + } + + node.setNext(head); + head = node; + return true; + } + + /** + * 出栈 + */ + public Integer pop() { + if (head == null) { + return null; + } + + Integer val = head.getVal(); + head = head.getNext(); + return val; + } +}