一)链表简介
链表概念:链表是一种物理存储单元上非连续、非顺序的存储结构,也就是说链表存储的数据不一定是有序的。
链表结构:链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
单链表:每一个结点中,包含一个数据域,一个指针域,该指针域一直指向下一个结点。
单链表缺点:查找某个元素时,要从头开始找起。
单链表图解:头部结点(head),数据域(data)、指针域(next),尾部结点next指向null。
二)单链表
第一步:初始化单链表结构
/**
* 自定义单链表
* @author ouyangjun
*/
public class SingleChainTable<E> {
static class Node<E> {
E data; // 数据域
Node<E> next; // 指针域,指向下一个结点
Node(E x) { data = x;}
}
private Node<E> head; // 指向头部结点
private final AtomicInteger count = new AtomicInteger(); // 记录结点数量,原子性操作
}
第二步:添加结点
场景一:在头部添加结点。新增一个结点a,把结点a的next指向结点a1,再把结点a本身作为head结点。
/** 从头部添加结点 */
public Node<E> addHead(E e) {
if (e == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(e); // 初始化一个新结点
if (head != null) { // 判断是否已存在结点
newNode.next = head; // 把新结点next指向头部结点
}
head = newNode; // 把新结点作为头部结点
// 累计结点数量
final AtomicInteger count = this.count;
count.getAndIncrement();
return newNode;
}
/** 单链表结点数量 */
public int size() {
return count.get();
}
场景二:在尾部添加结点。新增一个结点a,先从头部遍历找到尾部结点an,把结点an的next指向结点a即可。
/** 在尾部添加结点 */
public Node<E> addEnd(E e) {
if (e == null) throw new NullPointerException();
Node<E> newNode = new Node<E>(e); // 新结点
Node<E> headNode = head; // 辅助结点
if (headNode == null) {
head = newNode; // 将新结点添加到头部
} else {
while (headNode.next != null) { // 从头部开始遍历, 判断是否有下一结点
headNode = headNode.next;
}
headNode.next = newNode; // 将新结点添加到尾部
}
// 累计结点数量
final AtomicInteger count = this.count;
count.getAndIncrement();
return newNode;
}
场景三:在指定下标位置添加结点。在结点a2和结点a3之间添加一个结点a,先找到结点a2的位置,用临时辅助结点记录结点a2的位置,再用一个临时辅助结点记录节点a3的位置。在新增时,把临时辅助结点a2的next指向结点a,再把结点a的next指向临时辅助结点a3,即完成添加。(用到了两个临时的辅助结点)
/** 在指定位置添加结点 */
public Node<E> add(int index, E e) {
if (index < 0 || index > size()) throw new IllegalArgumentException();
if (e == null) throw new NullPointerException();
if (index == 0) { // 在头部添加
return addHead(e);
} else if (index == size()) { // 在尾部添加
return addEnd(e);
} else {
Node<E> newNode = new Node<E>(e); // 新结点
Node<E> headNode = head; // 辅助结点
Node<E> currentNode = head; // 辅助结点
int position = 0; // 临时下标,从头开始
while (currentNode != null) { // 判断是否有下一结点
// 判断下标和指定位置是否一致
if (index == position++) {
headNode.next = newNode; // 把找到的结点next指向新结点
newNode.next = currentNode; // 把新结点的next指向原先的下一结点
// 累计结点数量
final AtomicInteger count = this.count;
count.getAndIncrement();
// 跳出循环
break;
} else {
headNode = currentNode; // 标记当前的结点,记录结点a2的位置
currentNode = currentNode.next; // 标记下一结点,记录结点a3的位置
}
}
return newNode;
}
}
第三步:删除结点
场景一:删除头部结点。
/** 删除头部结点 */
public Node<E> removeHead() {
if (size() > 0) {
Node<E> headNode = head; // 临时头部结点
head = headNode.next; // 把临时头部结点的next结点作为新的头部结点
// 累减
count.getAndDecrement();
return headNode;
}
return null;
}
场景二:删除尾部结点。从头遍历,找到倒数第二个结点,把倒数第二个结点的next置null,相当于删除尾部结点an。
/** 删除尾部结点 */
public Node<E> removeEnd() {
int size = size();
if (size > 0) {
if (size == 1) { // 删除头部结点
return removeHead();
}
Node<E> headNode = head; // 辅助结点
Node<E> currentNode = head; // 辅助结点
while (currentNode.next != null) { // 从头部开始遍历, 判断是否有下一结点
headNode = currentNode;
currentNode = currentNode.next;
}
headNode.next = null; // 把尾部节点next置null,相当于删除
// 累减
count.getAndDecrement();
return currentNode;
}
return null;
}
场景三:删除指定下标位置的结点。删除结点a2,然后把结点a1的next指向结点a3即可。
/** 删除指定下标位置的结点 */
public Node<E> remove(int index) {
if (index < 0 || index > size()) throw new IllegalArgumentException();
if (index == 0) { // 删除头部结点
return removeHead();
} else if (index == size()) { // 删除尾部结点
return removeEnd();
} else {
Node<E> headNode = head;
Node<E> currentNode = head;
int position = 0; // 从头开始遍历
while (currentNode != null) { // 判断是否还有下一个结点
if (index == position++) {
headNode.next = currentNode.next;
currentNode = headNode;
// 累减
count.getAndDecrement();
break;
} else {
headNode = currentNode;
currentNode = currentNode.next;
}
}
return currentNode;
}
}
场景四:删除指定的结点data,和场景三类似。
/** 删除指定结点data */
public Node<E> remove(E e) {
if (e == null) throw new NullPointerException();
if (size() == 0) {
return null;
}
Node<E> currentNode = head; // 辅助结点
if (e.equals(currentNode.data)) {
return removeHead();
} else {
Node<E> headNode = head; // 辅助结点
while (currentNode != null) {
if (e.equals(currentNode.next.data)) { // 判断data是否一致
headNode = currentNode;
currentNode = currentNode.next;
// 累减
count.getAndDecrement();
break;
} else {
currentNode = currentNode.next;
headNode = headNode.next;
}
}
headNode.next = currentNode.next;
// 返回
return currentNode;
}
}
第四步:根据指定结点data查询是否存在
/** 根据指定结点data查询是否存在*/
public boolean get(E e) {
if (e == null) throw new NullPointerException();
Node<E> headNode = head; // 辅助节点
while (headNode != null) {
if (e.equals(headNode.data)) {
return true;
} else {
headNode = headNode.next;
}
}
return false;
}
第五步:正序输出所有结点data
/** 正序输出所有结点data */
public void inProperOrder() {
System.out.print("正序输出:\t");
Node<E> headNode = head;
while (headNode != null) {
System.out.print(headNode.data + "\t");
headNode = headNode.next;
}
}
第六步:利用Stack"先进后出、后进先出"的特性,高效逆序输出所有结点data
/** 利用Stack"先进后出、后进先出"的特性,高效逆序输出所有结点data */
public void invertedSequence() {
Stack<E> stack = new Stack<E>();
Node<E> headNode = head; // 临时辅助结点
while(headNode != null) {
stack.push(headNode.data); // 把结点data添加到Stack中
headNode = headNode.next;
}
System.out.print("利用Stack\"先进后出、后进先出\"的特性, 高效逆序输出:\t");
while (!stack.isEmpty()) {
System.out.print(stack.pop()+"\t");
}
}
识别二维码关注个人微信公众号
本章完结,待续,欢迎转载!
本文说明:该文章属于原创,如需转载,请标明文章转载来源!