master
ehlxr 2021-12-25 17:02:00 +08:00
parent 9107b0c4ba
commit c72ced990f
5 changed files with 327 additions and 41 deletions

View File

@ -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 + '}';
}
}

View File

@ -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);

View File

@ -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 != 00 与 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++];
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}