update at 2021-12-25 23:23:33 by ehlxr

master
ehlxr 2021-12-25 23:23:33 +08:00
parent 3aaabd8b6d
commit 987e56909c
5 changed files with 52 additions and 47 deletions

View File

@ -30,33 +30,33 @@ package io.github.ehlxr.datastructure;
* @author ehlxr * @author ehlxr
* @since 2021-12-23 14:41. * @since 2021-12-23 14:41.
*/ */
public class Node<K> { public class Node<T> {
private K val; private T val;
private Node<K> next; private Node<T> next;
public Node(K val, Node<K> next) { public Node(T val, Node<T> next) {
this.val = val; this.val = val;
this.next = next; this.next = next;
} }
public K getVal() { public T getVal() {
return val; return val;
} }
public void setVal(K val) { public void setVal(T val) {
this.val = val; this.val = val;
} }
public Node<K> getNext() { public Node<T> getNext() {
return next; return next;
} }
public void setNext(Node<K> next) { public void setNext(Node<T> next) {
this.next = next; this.next = next;
} }
public void print() { public void print() {
Node<K> n = this; Node<T> n = this;
while (n != null) { while (n != null) {
System.out.printf("%s -> ", n.getVal()); System.out.printf("%s -> ", n.getVal());
n = n.getNext(); n = n.getNext();

View File

@ -32,8 +32,9 @@ import io.github.ehlxr.datastructure.Node;
* @author ehlxr * @author ehlxr
* @since 2021-12-23 14:40. * @since 2021-12-23 14:40.
*/ */
public class ReverseLinkedList { public class ReverseLinkedList<T> {
public static void main(String[] args) { public static void main(String[] args) {
ReverseLinkedList<Integer> reverseLinkedList = new ReverseLinkedList<>();
Node<Integer> n5 = new Node<>(5, null); Node<Integer> n5 = new Node<>(5, null);
Node<Integer> n4 = new Node<>(4, n5); Node<Integer> n4 = new Node<>(4, n5);
Node<Integer> n3 = new Node<>(3, n4); Node<Integer> n3 = new Node<>(3, n4);
@ -45,22 +46,22 @@ public class ReverseLinkedList {
System.out.println("-------------"); System.out.println("-------------");
// head = reverse(head); // head = reverse(head);
head = reverse2(head); head = reverseLinkedList.reverse2(head);
// head = reverse3(head, null); // head = reverse3(head, null);
head.print(); head.print();
} }
// 循环方式 // 循环方式
public static Node<Integer> reverse(Node<Integer> head) { public Node<T> reverse(Node<T> head) {
if (head == null || head.getNext() == null) { if (head == null || head.getNext() == null) {
return head; return head;
} }
Node<Integer> pre = null; Node<T> pre = null;
Node<Integer> cur = head; Node<T> cur = head;
while (cur != null) { while (cur != null) {
Node<Integer> next = cur.getNext(); Node<T> next = cur.getNext();
cur.setNext(pre); cur.setNext(pre);
pre = cur; 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) { if (head == null || head.getNext() == null) {
return head; return head;
} }
Node<Integer> next = head.getNext(); Node<T> next = head.getNext();
Node<Integer> node = reverse2(next); Node<T> node = reverse2(next);
head.getNext().setNext(head); head.getNext().setNext(head);
head.setNext(null); 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) { if (head == null) {
return pre; return pre;
} }
Node<Integer> next = head.getNext(); Node<T> next = head.getNext();
Node<Integer> node = reverse3(next, head); Node<T> node = reverse3(next, head);
head.setNext(pre); head.setNext(pre);

View File

@ -34,11 +34,11 @@ import io.github.ehlxr.datastructure.Node;
* @author ehlxr * @author ehlxr
* @since 2021-12-25 16:02. * @since 2021-12-25 16:02.
*/ */
public class LinkedListQueue { public class LinkedListQueue<T> {
private Node<Integer> head; private Node<T> head;
public static void main(String[] args) { public static void main(String[] args) {
LinkedListQueue queue = new LinkedListQueue(); LinkedListQueue<Integer> 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));
} }
@ -55,15 +55,15 @@ public class LinkedListQueue {
* *
* O(n) * O(n)
*/ */
public boolean enqueue(Integer item) { public boolean enqueue(T item) {
Node<Integer> node = new Node<>(item, null); Node<T> node = new Node<T>(item, null);
if (head == null) { if (head == null) {
head = node; head = node;
return true; return true;
} }
// 找到队尾 // 找到队尾
Node<Integer> tail = head; Node<T> tail = head;
while (tail.getNext() != null) { while (tail.getNext() != null) {
tail = tail.getNext(); tail = tail.getNext();
} }
@ -75,18 +75,18 @@ public class LinkedListQueue {
/** /**
* *
*/ */
public Integer dequeue() { public T dequeue() {
if (head == null) { if (head == null) {
return null; return null;
} }
Integer val = head.getVal(); T val = head.getVal();
head = head.getNext(); head = head.getNext();
return val; return val;
} }
public Node<Integer> getData() { public Node<T> getData() {
return head; return head;
} }
} }

View File

@ -34,12 +34,12 @@ import io.github.ehlxr.datastructure.Node;
* @author ehlxr * @author ehlxr
* @since 2021-12-25 16:48. * @since 2021-12-25 16:48.
*/ */
public class LinkedListQueue2 { public class LinkedListQueue2<T> {
private Node<Integer> head = null; private Node<T> head = null;
private Node<Integer> tail = null; private Node<T> tail = null;
public static void main(String[] args) { public static void main(String[] args) {
LinkedListQueue2 queue = new LinkedListQueue2(); LinkedListQueue2<Integer> 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));
} }
@ -56,8 +56,8 @@ public class LinkedListQueue2 {
* *
* O(1) * O(1)
*/ */
public boolean enqueue(Integer item) { public boolean enqueue(T item) {
Node<Integer> node = new Node<Integer>(item, null); Node<T> node = new Node<>(item, null);
if (tail == null) { if (tail == null) {
head = node; head = node;
@ -73,12 +73,12 @@ public class LinkedListQueue2 {
/** /**
* *
*/ */
public Integer dequeue() { public T dequeue() {
if (head == null) { if (head == null) {
return null; return null;
} }
Integer val = head.getVal(); T val = head.getVal();
head = head.getNext(); head = head.getNext();
// 如果队列为空了,需要把 tail 置空 // 如果队列为空了,需要把 tail 置空
if (head == null) { if (head == null) {
@ -88,7 +88,7 @@ public class LinkedListQueue2 {
return val; return val;
} }
public Node<Integer> getData() { public Node<T> getData() {
return head; return head;
} }
} }

View File

@ -30,15 +30,15 @@ import io.github.ehlxr.datastructure.Node;
* @author ehlxr * @author ehlxr
* @since 2021-12-25 22:28. * @since 2021-12-25 22:28.
*/ */
public class LinkedListStack { public class LinkedListStack<T> {
private Node<Integer> head; private Node<T> head;
public Node<Integer> getData() { public Node<T> getData() {
return head; return head;
} }
public static void main(String[] args) { public static void main(String[] args) {
LinkedListStack stack = new LinkedListStack(); LinkedListStack<Integer> stack = new LinkedListStack<>();
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
System.out.println(stack.push(i)); System.out.println(stack.push(i));
} }
@ -53,8 +53,8 @@ public class LinkedListStack {
/** /**
* *
*/ */
public boolean push(Integer item) { public boolean push(T item) {
Node<Integer> node = new Node<>(item, null); Node<T> node = new Node<>(item, null);
if (head == null) { if (head == null) {
head = node; head = node;
return true; return true;
@ -68,13 +68,17 @@ public class LinkedListStack {
/** /**
* *
*/ */
public Integer pop() { public T pop() {
if (head == null) { if (head == null) {
return null; return null;
} }
Integer val = head.getVal(); T val = head.getVal();
head = head.getNext(); head = head.getNext();
return val; return val;
} }
public void clear() {
head = null;
}
} }