学一学单链表
描述
一组任意储存单元(可连续、可不连续)组成的线性表
特点
因为不是连续存储结构,所以需要再存储单元的元素中存储对应的逻辑关系(即存储它后续单元的地址)
结构图
循环截至条件
节点的指针指向是否等于NULL
JAVA代码实现
package com.example.demo.data;
import java.util.Objects;
public class SingleLinkList<T> {
public static void main(String[] args) {
SingleLinkList<Object> listNode = new SingleLinkList<>();
System.out.println("======新增=====");
System.out.println("增加前元素情况:" + listNode.toString());
System.out.println("增加前长度:" + listNode.count);
for (int i = 0; i < 10; i++) {
listNode.add(i);
}
System.out.println("增加前长度:" + listNode.count);
System.out.println("增加后元素情况:" + listNode.toString());
System.out.println("======删除=====");
Object removeByIndex = listNode.remove(9);
System.out.println("通过下标删除的元素为:" + removeByIndex.toString());
System.out.println("通过下标删除之后元素情况:" + listNode.toString());
System.out.println("通过下标删除之后长度:" + listNode.count);
listNode.remove(new Integer("8"));
System.out.println("通过数据删除之后元素情况:" + listNode.toString());
System.out.println("通过数据删除之后长度:" + listNode.count);
System.out.println("======查询=====");
System.out.println("通过下标查询" + listNode.get(7).data);
System.out.println("通过数据查询" + listNode.get(1).data);
System.out.println("======通过下标修改=====");
System.out.println("通过下标替换数据替换前0标数据" + listNode.get(0).data);
System.out.println("通过下标替换数据替换0标数据为100:" + listNode.replace(0, new Integer(100)));
System.out.println("通过下标替换数据替换后0标数据" + listNode.get(0).data);
System.out.println("======通过数据项修改=====");
System.out.println("先查询是否包含数据1000是否存在:" + listNode.get(new Integer("1000")));
System.out.println("通过数据项替换数据替将100替换为1000:" + listNode.replace(new Integer(100), new Integer(1000)));
System.out.println("替换后查询数据1000是否存在:" + listNode.get(new Integer("1000")).data);
}
private int count;
private Node<T> root;
public SingleLinkList() {
}
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private boolean isPositionIndex(int index) {
return index >= 0 && index <= count - 1;
}
private String outOfBoundsMsg(int index) {
return "Index: " + index + ", Count: " + (count - 1);
}
private static class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
this.data = data;
}
public void add(T data) {
Node<T> node = this;
while (node.next != null) {
node = node.next;
}
node.next = new Node<T>(data);
}
public Node<T> get(int index) {
Node<T> node = this;
for (int i = 0; i < index; i++) {
node = node.next;
}
return node;
}
public T remove(int index) {
Node<T> pre = this;
for (int i = 0; i < index - 1; i++)
pre = pre.next;
Node<T> current = pre.next;
T data = current.data;
pre.next = current.next;
current.next = null;
current.data = null;
return data;
}
public boolean remove(T data) {
Node<T> pre = this;
Node<T> current = pre.next;
while (current != null) {
if (Objects.equals(data, current.data)) {
pre.next = current.next;
current.next = null;
current.data = null;
return true;
}
pre = pre.next;
current = pre.next;
}
return false;
}
public Node<T> get(T data) {
for (Node<T> node = this; node != null; node = node.next) {
if (Objects.equals(data, node.data)) {
return node;
}
}
return null;
}
}
void add(T data) {
if (isEmpty()) {
this.root = new Node<>(data);
} else {
this.root.add(data);
}
this.count++;
}
public T remove(int index) {
checkPositionIndex(index);
if (isEmpty()) {
return null;
}
T result;
if (index == 0) {
Node<T> temp = this.root;
result = this.root.data;
this.root = root.next;
temp.next = null;
temp.data = null;
} else {
result = this.root.remove(index);
}
count--;
return result;
}
public void remove(T data) {
if (isEmpty()) {
return;
}
if (Objects.equals(data, this.root.data)) {
Node<T> temp = root;
root = root.next;
temp.next = null;
temp.data = null;
this.count--;
} else {
boolean remove = this.root.remove(data);
if (remove) {
this.count--;
}
}
}
public Node<T> get(int index) {
checkPositionIndex(index);
if (this.isEmpty()) {
return null;
}
return this.root.get(index);
}
public Node<T> get(T data) {
if (this.isEmpty()) {
return null;
}
return this.root.get(data);
}
public boolean replace(int index, T data) {
Node<T> tNode = this.get(index);
if (null != tNode) {
tNode.data = data;
return true;
}
return false;
}
public boolean replace(T oldData, T newData) {
Node<T> tNode = this.get(oldData);
if (null != tNode) {
tNode.data = newData;
return true;
}
return false;
}
@Override
public String toString() {
StringBuffer result = new StringBuffer();
for (Node<T> node = this.root; node != null; node = node.next) {
result.append(node.data);
result.append(" ");
}
return result.toString();
}
boolean isEmpty() {
return count == 0 || this.root == null;
}
}