定义
在计算机科学中,链表是数据元素的线性集合,其每个元素都指向下一个元素,元素存储上并不连续。
双向链表,每个元素知道其上一个元素和下一个元素。
以下为示例代码:
package com.tfq.arithmetic.linkedlist;
import java.util.Iterator;
import java.util.function.Consumer;
/**
* @author: fqtang
* @date: 2024/05/21/9:29
* @description: 双向链表(带哨兵)
*/
public class DoubleLinkedListSentinel implements Iterable<Integer> {
static class Node {
Node prev;//上一个节点指针
int value;//值
Node next;//下一个节点指针
public Node(Node prev, int value, Node next) {
this.prev = prev;
this.next = next;
this.value = value;
}
}
private Node head;//头哨兵
private Node tail;//尾哨兵
public DoubleLinkedListSentinel() {
this.head = new Node(null, 666, null);
this.tail = new Node(null, 999, null);
head.next = tail;
tail.prev = head;
}
/**
* 返回链表第一种方式
*
* @r