主要实现了增删查以及翻转。改比较简单就没写。把前面的组合一下就可以做出来了。
自测没有问题。如果发现存在BUG,请指正。
package linklist;
import java.util.Objects;
import java.util.Scanner;
/**
* 节点类,用于定义节点的内容
*
* @param <T> 使用时指定类型,注意类型不可以为基本类型。
* @date 2020-12-16 10:45
*/
class Node<T> {
T value;
Node<T> next;
public Node(T value, Node<T> next) {
this.value = value;
this.next = next;
}
}
/**
* 链表类,用于实现链表的相关操作
*
* @author SQH
*/
class LinkList<T> {
private int length = 0;
private Node<T> head;
public LinkList() {
head = new Node<>(null, null);
}
public boolean isEmpty() {
return head.next == null;
}
public Node<T> getNodeByLocation(int location) {
if (location < 1) {
throw new RuntimeException("数字错误,位置不可以小于1\n");
}
Node<T> p = head.next;
int count = 1;
while (p != null && count != location) {
count++;
p = p.next;
}
return p;
}
public Node<T> getNodeByNum(T i) {
Node<T> p = head.next;
while (p != null && p.value != i) {
p = p.next;
}
return p;
}
public void addNodeFront(T value) {
Node<T> p = head;
Node<T> newNode = new Node<>(value, null);
newNode.next = p.next;
p.next = newNode;
length++;
}
public void addNodeBack(T value) {
Node<T> p = head;
while (p.next != null) {
p = p.next;
}
p.next = new Node<>(value, null);
length++;
}
public void removeByNum(T value) {
Node<T> p = head;
while (p.next != null && p.next.value != value) {
p = p.next;
}
if (p.next != null) {
p.next = p.next.next;
length--;
}
}
public void removeByLocation(int location) {
if (location > length) {
return;
}
Node<T> p = head;
for (int i = 0; i < location - 1 && p.next != null; i++) {
p = p.next;
}
if (p.next != null) {
p.next = p.next.next;
length--;
}
}
public int getLength() {
return length;
}
public void show() {
Node<T> p = head.next;
while (p != null) {
System.out.print(p.value + "\t");
p = p.next;
}
System.out.println();
}
public void destroy() {
head.next = null;
length = 0;
}
/**
* 自身原地反转,不返回额外的结点
*/
public void reverse() {
Node<T> pre = null;
Node<T> current = head.next;
while (current != null) {
Node<T> temp = current.next;
current.next = pre;
pre = current;
current = temp;
}
head = new Node<>(null, pre);
}
}
/**
* @author SQH
*/
public class LinkListTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
LinkList<Integer> head = new LinkList<>();
for (int i = 0; i < 20; i++) {
head.addNodeFront(i);
}
System.out.println("isEmpty: " + head.isEmpty());
System.out.println("Original list: ");
head.show();
System.out.println("Current length: " + head.getLength() + "\n");
head.removeByLocation(5);
System.out.println("After removing node: ");
head.show();
System.out.println("Current length: " + head.getLength() + "\n");
head.removeByNum(9);
System.out.println("After removing node: ");
head.show();
System.out.println("Current length: " + head.getLength() + "\n");
head.addNodeBack(4);
System.out.println("After adding node: ");
head.show();
System.out.println("Current length: " + head.getLength() + "\n");
System.out.print("input the number you want to find: ");
Node<Integer> node = head.getNodeByNum(in.nextInt());
System.out.println(Objects.requireNonNullElse(node, "No matched node!") + "\n");
try {
System.out.print("input the node location you want to find: ");
Node<Integer> locationNode = head.getNodeByLocation(in.nextInt());
System.out.println(Objects.requireNonNullElse(locationNode, "No matched node!") + "\n");
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
System.out.print("before reverse: ");
head.show();
System.out.print("after reverse: ");
head.reverse();
head.show();
}
}