update at 2021-12-25 23:23:33 by ehlxr
parent
3aaabd8b6d
commit
987e56909c
|
@ -30,33 +30,33 @@ package io.github.ehlxr.datastructure;
|
|||
* @author ehlxr
|
||||
* @since 2021-12-23 14:41.
|
||||
*/
|
||||
public class Node<K> {
|
||||
private K val;
|
||||
private Node<K> next;
|
||||
public class Node<T> {
|
||||
private T val;
|
||||
private Node<T> next;
|
||||
|
||||
public Node(K val, Node<K> next) {
|
||||
public Node(T val, Node<T> 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<K> getNext() {
|
||||
public Node<T> getNext() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(Node<K> next) {
|
||||
public void setNext(Node<T> next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public void print() {
|
||||
Node<K> n = this;
|
||||
Node<T> n = this;
|
||||
while (n != null) {
|
||||
System.out.printf("%s -> ", n.getVal());
|
||||
n = n.getNext();
|
||||
|
|
|
@ -32,8 +32,9 @@ import io.github.ehlxr.datastructure.Node;
|
|||
* @author ehlxr
|
||||
* @since 2021-12-23 14:40.
|
||||
*/
|
||||
public class ReverseLinkedList {
|
||||
public class ReverseLinkedList<T> {
|
||||
public static void main(String[] args) {
|
||||
ReverseLinkedList<Integer> reverseLinkedList = new ReverseLinkedList<>();
|
||||
Node<Integer> n5 = new Node<>(5, null);
|
||||
Node<Integer> n4 = new Node<>(4, n5);
|
||||
Node<Integer> 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<Integer> reverse(Node<Integer> head) {
|
||||
public Node<T> reverse(Node<T> head) {
|
||||
if (head == null || head.getNext() == null) {
|
||||
return head;
|
||||
}
|
||||
Node<Integer> pre = null;
|
||||
Node<T> pre = null;
|
||||
|
||||
Node<Integer> cur = head;
|
||||
Node<T> cur = head;
|
||||
while (cur != null) {
|
||||
Node<Integer> next = cur.getNext();
|
||||
Node<T> next = cur.getNext();
|
||||
cur.setNext(pre);
|
||||
pre = cur;
|
||||
|
||||
|
@ -71,13 +72,13 @@ public class ReverseLinkedList {
|
|||
}
|
||||
|
||||
// 递归一
|
||||
public static Node<Integer> reverse2(Node<Integer> head) {
|
||||
public Node<T> reverse2(Node<T> head) {
|
||||
if (head == null || head.getNext() == null) {
|
||||
return head;
|
||||
}
|
||||
|
||||
Node<Integer> next = head.getNext();
|
||||
Node<Integer> node = reverse2(next);
|
||||
Node<T> next = head.getNext();
|
||||
Node<T> node = reverse2(next);
|
||||
|
||||
head.getNext().setNext(head);
|
||||
head.setNext(null);
|
||||
|
@ -86,13 +87,13 @@ public class ReverseLinkedList {
|
|||
}
|
||||
|
||||
// 递归二
|
||||
public static Node<Integer> reverse3(Node<Integer> head, Node<Integer> pre) {
|
||||
public Node<T> reverse3(Node<T> head, Node<T> pre) {
|
||||
if (head == null) {
|
||||
return pre;
|
||||
}
|
||||
|
||||
Node<Integer> next = head.getNext();
|
||||
Node<Integer> node = reverse3(next, head);
|
||||
Node<T> next = head.getNext();
|
||||
Node<T> node = reverse3(next, head);
|
||||
|
||||
head.setNext(pre);
|
||||
|
||||
|
|
|
@ -34,11 +34,11 @@ import io.github.ehlxr.datastructure.Node;
|
|||
* @author ehlxr
|
||||
* @since 2021-12-25 16:02.
|
||||
*/
|
||||
public class LinkedListQueue {
|
||||
private Node<Integer> head;
|
||||
public class LinkedListQueue<T> {
|
||||
private Node<T> head;
|
||||
|
||||
public static void main(String[] args) {
|
||||
LinkedListQueue queue = new LinkedListQueue();
|
||||
LinkedListQueue<Integer> 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<Integer> node = new Node<>(item, null);
|
||||
public boolean enqueue(T item) {
|
||||
Node<T> node = new Node<T>(item, null);
|
||||
if (head == null) {
|
||||
head = node;
|
||||
return true;
|
||||
}
|
||||
|
||||
// 找到队尾
|
||||
Node<Integer> tail = head;
|
||||
Node<T> 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<Integer> getData() {
|
||||
public Node<T> getData() {
|
||||
return head;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,12 +34,12 @@ import io.github.ehlxr.datastructure.Node;
|
|||
* @author ehlxr
|
||||
* @since 2021-12-25 16:48.
|
||||
*/
|
||||
public class LinkedListQueue2 {
|
||||
private Node<Integer> head = null;
|
||||
private Node<Integer> tail = null;
|
||||
public class LinkedListQueue2<T> {
|
||||
private Node<T> head = null;
|
||||
private Node<T> tail = null;
|
||||
|
||||
public static void main(String[] args) {
|
||||
LinkedListQueue2 queue = new LinkedListQueue2();
|
||||
LinkedListQueue2<Integer> 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<Integer> node = new Node<Integer>(item, null);
|
||||
public boolean enqueue(T item) {
|
||||
Node<T> 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<Integer> getData() {
|
||||
public Node<T> getData() {
|
||||
return head;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,15 +30,15 @@ import io.github.ehlxr.datastructure.Node;
|
|||
* @author ehlxr
|
||||
* @since 2021-12-25 22:28.
|
||||
*/
|
||||
public class LinkedListStack {
|
||||
private Node<Integer> head;
|
||||
public class LinkedListStack<T> {
|
||||
private Node<T> head;
|
||||
|
||||
public Node<Integer> getData() {
|
||||
public Node<T> getData() {
|
||||
return head;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
LinkedListStack stack = new LinkedListStack();
|
||||
LinkedListStack<Integer> 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<Integer> node = new Node<>(item, null);
|
||||
public boolean push(T item) {
|
||||
Node<T> 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue