update at 2021-12-25 22:49:21 by ehlxr

This commit is contained in:
ehlxr 2021-12-25 22:49:21 +08:00
parent 1b10985030
commit 3aaabd8b6d
6 changed files with 197 additions and 39 deletions

View File

@ -22,7 +22,7 @@
* THE SOFTWARE. * 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 * @author ehlxr
* @since 2021-12-23 14:41. * @since 2021-12-23 14:41.
*/ */
public class Node { public class Node<K> {
private Integer val; private K val;
private Node next; private Node<K> next;
public Node(Integer val, Node next) { public Node(K val, Node<K> next) {
this.val = val; this.val = val;
this.next = next; this.next = next;
} }
public Integer getVal() { public K getVal() {
return val; return val;
} }
public void setVal(Integer val) { public void setVal(K val) {
this.val = val; this.val = val;
} }
public Node getNext() { public Node<K> getNext() {
return next; return next;
} }
public void setNext(Node next) { public void setNext(Node<K> next) {
this.next = next; this.next = next;
} }
public void print() { public void print() {
Node n = this; Node<K> n = this;
while (n != null) { while (n != null) {
System.out.printf("%s -> ", n); System.out.printf("%s -> ", n.getVal());
n = n.getNext(); n = n.getNext();
} }
System.out.print("null");
System.out.println(); System.out.println();
} }

View File

@ -24,6 +24,8 @@
package io.github.ehlxr.datastructure.linkedlist; 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 class ReverseLinkedList {
public static void main(String[] args) { public static void main(String[] args) {
Node n5 = new Node(5, null); Node<Integer> n5 = new Node<>(5, null);
Node n4 = new Node(4, n5); Node<Integer> n4 = new Node<>(4, n5);
Node n3 = new Node(3, n4); Node<Integer> n3 = new Node<>(3, n4);
Node n2 = new Node(2, n3); Node<Integer> n2 = new Node<>(2, n3);
Node n1 = new Node(1, n2); Node<Integer> n1 = new Node<>(1, n2);
Node head = new Node(0, n1); Node<Integer> head = new Node<>(0, n1);
head.print(); head.print();
@ -50,15 +52,15 @@ public class ReverseLinkedList {
// 循环方式 // 循环方式
public static Node reverse(Node head) { public static Node<Integer> reverse(Node<Integer> head) {
if (head == null || head.getNext() == null) { if (head == null || head.getNext() == null) {
return head; return head;
} }
Node pre = null; Node<Integer> pre = null;
Node cur = head; Node<Integer> cur = head;
while (cur != null) { while (cur != null) {
Node next = cur.getNext(); Node<Integer> next = cur.getNext();
cur.setNext(pre); cur.setNext(pre);
pre = cur; pre = cur;
@ -69,13 +71,13 @@ public class ReverseLinkedList {
} }
// 递归一 // 递归一
public static Node reverse2(Node head) { public static Node<Integer> reverse2(Node<Integer> head) {
if (head == null || head.getNext() == null) { if (head == null || head.getNext() == null) {
return head; return head;
} }
Node next = head.getNext(); Node<Integer> next = head.getNext();
Node node = reverse2(next); Node<Integer> node = reverse2(next);
head.getNext().setNext(head); head.getNext().setNext(head);
head.setNext(null); head.setNext(null);
@ -84,13 +86,13 @@ public class ReverseLinkedList {
} }
// 递归二 // 递归二
public static Node reverse3(Node head, Node pre) { public static Node<Integer> reverse3(Node<Integer> head, Node<Integer> pre) {
if (head == null) { if (head == null) {
return pre; return pre;
} }
Node next = head.getNext(); Node<Integer> next = head.getNext();
Node node = reverse3(next, head); Node<Integer> node = reverse3(next, head);
head.setNext(pre); head.setNext(pre);

View File

@ -24,7 +24,7 @@
package io.github.ehlxr.datastructure.queue; 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. * @since 2021-12-25 16:02.
*/ */
public class LinkedListQueue { public class LinkedListQueue {
private Node head; private Node<Integer> head;
public static void main(String[] args) { public static void main(String[] args) {
LinkedListQueue queue = new LinkedListQueue(); LinkedListQueue queue = new LinkedListQueue();
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
System.out.println(queue.enqueue(i)); System.out.println(queue.enqueue(i));
queue.getData().print();
} }
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
@ -57,14 +56,14 @@ public class LinkedListQueue {
* 时间复杂度 O(n) * 时间复杂度 O(n)
*/ */
public boolean enqueue(Integer item) { public boolean enqueue(Integer item) {
Node node = new Node(item, null); Node<Integer> node = new Node<>(item, null);
if (head == null) { if (head == null) {
head = node; head = node;
return true; return true;
} }
// 找到队尾 // 找到队尾
Node tail = head; Node<Integer> tail = head;
while (tail.getNext() != null) { while (tail.getNext() != null) {
tail = tail.getNext(); tail = tail.getNext();
} }
@ -87,7 +86,7 @@ public class LinkedListQueue {
return val; return val;
} }
public Node getData() { public Node<Integer> getData() {
return head; return head;
} }
} }

View File

@ -24,7 +24,7 @@
package io.github.ehlxr.datastructure.queue; 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. * @since 2021-12-25 16:48.
*/ */
public class LinkedListQueue2 { public class LinkedListQueue2 {
private Node head = null; private Node<Integer> head = null;
private Node tail = null; private Node<Integer> tail = null;
public static void main(String[] args) { public static void main(String[] args) {
LinkedListQueue2 queue = new LinkedListQueue2(); LinkedListQueue2 queue = new LinkedListQueue2();
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
System.out.println(queue.enqueue(i)); System.out.println(queue.enqueue(i));
queue.getData().print();
} }
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
@ -58,7 +57,7 @@ public class LinkedListQueue2 {
* 时间复杂度 O(1) * 时间复杂度 O(1)
*/ */
public boolean enqueue(Integer item) { public boolean enqueue(Integer item) {
Node node = new Node(item, null); Node<Integer> node = new Node<Integer>(item, null);
if (tail == null) { if (tail == null) {
head = node; head = node;
@ -89,7 +88,7 @@ public class LinkedListQueue2 {
return val; return val;
} }
public Node getData() { public Node<Integer> getData() {
return head; return head;
} }
} }

View File

@ -0,0 +1,77 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 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.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];
}
}

View File

@ -0,0 +1,80 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 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.stack;
import io.github.ehlxr.datastructure.Node;
/**
* @author ehlxr
* @since 2021-12-25 22:28.
*/
public class LinkedListStack {
private Node<Integer> head;
public Node<Integer> 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<Integer> 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;
}
}