From 987e56909c36a7ba11f54fec92085c05ed002415 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Sat, 25 Dec 2021 23:23:33 +0800 Subject: [PATCH] update at 2021-12-25 23:23:33 by ehlxr --- .../io/github/ehlxr/datastructure/Node.java | 18 ++++++------- .../linkedlist/ReverseLinkedList.java | 25 ++++++++++--------- .../datastructure/queue/LinkedListQueue.java | 18 ++++++------- .../datastructure/queue/LinkedListQueue2.java | 18 ++++++------- .../datastructure/stack/LinkedListStack.java | 20 +++++++++------ 5 files changed, 52 insertions(+), 47 deletions(-) diff --git a/budd-common/src/main/java/io/github/ehlxr/datastructure/Node.java b/budd-common/src/main/java/io/github/ehlxr/datastructure/Node.java index b0fc89f..669d327 100644 --- a/budd-common/src/main/java/io/github/ehlxr/datastructure/Node.java +++ b/budd-common/src/main/java/io/github/ehlxr/datastructure/Node.java @@ -30,33 +30,33 @@ package io.github.ehlxr.datastructure; * @author ehlxr * @since 2021-12-23 14:41. */ -public class Node { - private K val; - private Node next; +public class Node { + private T val; + private Node next; - public Node(K val, Node next) { + public Node(T val, Node next) { this.val = val; this.next = next; } - public K getVal() { + public T getVal() { return val; } - public void setVal(K val) { + public void setVal(T 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.getVal()); n = n.getNext(); 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 4c7f8a3..7d03083 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,8 +32,9 @@ import io.github.ehlxr.datastructure.Node; * @author ehlxr * @since 2021-12-23 14:40. */ -public class ReverseLinkedList { +public class ReverseLinkedList { public static void main(String[] args) { + ReverseLinkedList reverseLinkedList = new ReverseLinkedList<>(); Node n5 = new Node<>(5, null); Node n4 = new Node<>(4, n5); Node n3 = new Node<>(3, n4); @@ -45,22 +46,22 @@ public class ReverseLinkedList { System.out.println("-------------"); // head = reverse(head); - head = reverse2(head); + head = reverseLinkedList.reverse2(head); // head = reverse3(head, null); head.print(); } // 循环方式 - public static Node reverse(Node head) { + public 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; @@ -71,13 +72,13 @@ public class ReverseLinkedList { } // 递归一 - public static Node reverse2(Node head) { + public 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); @@ -86,13 +87,13 @@ public class ReverseLinkedList { } // 递归二 - public static Node reverse3(Node head, Node pre) { + public 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 26e19fc..724850c 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 @@ -34,11 +34,11 @@ import io.github.ehlxr.datastructure.Node; * @author ehlxr * @since 2021-12-25 16:02. */ -public class LinkedListQueue { - private Node head; +public class LinkedListQueue { + private Node head; public static void main(String[] args) { - LinkedListQueue queue = new LinkedListQueue(); + LinkedListQueue queue = new LinkedListQueue<>(); for (int i = 0; i < 6; i++) { System.out.println(queue.enqueue(i)); } @@ -55,15 +55,15 @@ public class LinkedListQueue { * 入队(从队尾添加元素) * 时间复杂度 O(n) */ - public boolean enqueue(Integer item) { - Node node = new Node<>(item, null); + public boolean enqueue(T item) { + 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(); } @@ -75,18 +75,18 @@ public class LinkedListQueue { /** * 出队(从队首取出元素) */ - public Integer dequeue() { + public T dequeue() { if (head == null) { return null; } - Integer val = head.getVal(); + T val = head.getVal(); head = head.getNext(); 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 2720612..fc7d84d 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 @@ -34,12 +34,12 @@ import io.github.ehlxr.datastructure.Node; * @author ehlxr * @since 2021-12-25 16:48. */ -public class LinkedListQueue2 { - private Node head = null; - private Node tail = null; +public class LinkedListQueue2 { + private Node head = null; + private Node tail = null; public static void main(String[] args) { - LinkedListQueue2 queue = new LinkedListQueue2(); + LinkedListQueue2 queue = new LinkedListQueue2<>(); for (int i = 0; i < 6; i++) { System.out.println(queue.enqueue(i)); } @@ -56,8 +56,8 @@ public class LinkedListQueue2 { * 入队(从队尾添加元素) * 时间复杂度 O(1) */ - public boolean enqueue(Integer item) { - Node node = new Node(item, null); + public boolean enqueue(T item) { + Node node = new Node<>(item, null); if (tail == null) { head = node; @@ -73,12 +73,12 @@ public class LinkedListQueue2 { /** * 出队(从队首取出元素) */ - public Integer dequeue() { + public T dequeue() { if (head == null) { return null; } - Integer val = head.getVal(); + T val = head.getVal(); head = head.getNext(); // 如果队列为空了,需要把 tail 置空 if (head == null) { @@ -88,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/LinkedListStack.java b/budd-common/src/main/java/io/github/ehlxr/datastructure/stack/LinkedListStack.java index a714061..3e95f59 100644 --- 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 @@ -30,15 +30,15 @@ import io.github.ehlxr.datastructure.Node; * @author ehlxr * @since 2021-12-25 22:28. */ -public class LinkedListStack { - private Node head; +public class LinkedListStack { + private Node head; - public Node getData() { + public Node getData() { return head; } public static void main(String[] args) { - LinkedListStack stack = new LinkedListStack(); + LinkedListStack stack = new LinkedListStack<>(); for (int i = 0; i < 6; i++) { System.out.println(stack.push(i)); } @@ -53,8 +53,8 @@ public class LinkedListStack { /** * 入栈 */ - public boolean push(Integer item) { - Node node = new Node<>(item, null); + public boolean push(T item) { + Node node = new Node<>(item, null); if (head == null) { head = node; return true; @@ -68,13 +68,17 @@ public class LinkedListStack { /** * 出栈 */ - public Integer pop() { + public T pop() { if (head == null) { return null; } - Integer val = head.getVal(); + T val = head.getVal(); head = head.getNext(); return val; } + + public void clear() { + head = null; + } }