refactor
parent
9107b0c4ba
commit
c72ced990f
|
@ -30,35 +30,42 @@ package io.github.ehlxr.datastructure.linkedlist;
|
||||||
* @author ehlxr
|
* @author ehlxr
|
||||||
* @since 2021-12-23 14:41.
|
* @since 2021-12-23 14:41.
|
||||||
*/
|
*/
|
||||||
public class ListNode {
|
public class Node {
|
||||||
private int val;
|
private Integer val;
|
||||||
private ListNode next;
|
private Node next;
|
||||||
|
|
||||||
public ListNode(int val, ListNode next) {
|
public Node(Integer val, Node next) {
|
||||||
this.val = val;
|
this.val = val;
|
||||||
this.next = next;
|
this.next = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getVal() {
|
public Integer getVal() {
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVal(int val) {
|
public void setVal(Integer val) {
|
||||||
this.val = val;
|
this.val = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ListNode getNext() {
|
public Node getNext() {
|
||||||
return next;
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNext(ListNode next) {
|
public void setNext(Node next) {
|
||||||
this.next = 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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "SingleNode{" +
|
return "Node{" + val + '}';
|
||||||
"val=" + val +
|
|
||||||
'}';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -32,45 +32,33 @@ package io.github.ehlxr.datastructure.linkedlist;
|
||||||
*/
|
*/
|
||||||
public class ReverseLinkedList {
|
public class ReverseLinkedList {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ListNode n5 = new ListNode(5, null);
|
Node n5 = new Node(5, null);
|
||||||
ListNode n4 = new ListNode(4, n5);
|
Node n4 = new Node(4, n5);
|
||||||
ListNode n3 = new ListNode(3, n4);
|
Node n3 = new Node(3, n4);
|
||||||
ListNode n2 = new ListNode(2, n3);
|
Node n2 = new Node(2, n3);
|
||||||
ListNode n1 = new ListNode(1, n2);
|
Node n1 = new Node(1, n2);
|
||||||
ListNode head = new ListNode(0, n1);
|
Node head = new Node(0, n1);
|
||||||
|
|
||||||
print(head);
|
head.print();
|
||||||
|
|
||||||
System.out.println("-------------");
|
System.out.println("-------------");
|
||||||
// head = reverse(head);
|
// head = reverse(head);
|
||||||
head = reverse2(head);
|
head = reverse2(head);
|
||||||
// head = reverse3(head, null);
|
// 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) {
|
if (head == null || head.getNext() == null) {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
ListNode pre = null;
|
Node pre = null;
|
||||||
|
|
||||||
ListNode cur = head;
|
Node cur = head;
|
||||||
while (cur != null) {
|
while (cur != null) {
|
||||||
ListNode next = cur.getNext();
|
Node next = cur.getNext();
|
||||||
cur.setNext(pre);
|
cur.setNext(pre);
|
||||||
pre = cur;
|
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) {
|
if (head == null || head.getNext() == null) {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
ListNode next = head.getNext();
|
Node next = head.getNext();
|
||||||
ListNode node = reverse2(next);
|
Node node = reverse2(next);
|
||||||
|
|
||||||
head.getNext().setNext(head);
|
head.getNext().setNext(head);
|
||||||
head.setNext(null);
|
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) {
|
if (head == null) {
|
||||||
return pre;
|
return pre;
|
||||||
}
|
}
|
||||||
|
|
||||||
ListNode next = head.getNext();
|
Node next = head.getNext();
|
||||||
ListNode node = reverse3(next, head);
|
Node node = reverse3(next, head);
|
||||||
|
|
||||||
head.setNext(pre);
|
head.setNext(pre);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,103 @@
|
||||||
|
/*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright © 2021 xrv <xrg@live.com>
|
||||||
|
*
|
||||||
|
* 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++];
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,93 @@
|
||||||
|
/*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright © 2021 xrv <xrg@live.com>
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基于链表实现的队列
|
||||||
|
* <p>
|
||||||
|
* 入队时间复杂度 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,95 @@
|
||||||
|
/*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright © 2021 xrv <xrg@live.com>
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 基于链表实现的队列
|
||||||
|
* <p>
|
||||||
|
* 多一个变量 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue