双向环形链表带哨兵,这时哨兵既作为头,也作为尾。
package com.tfq.arithmetic.linkedlist;
import java.util.Iterator;
/**
* @author: fqtang
* @date: 2024/05/22/8:40
* @description: 环形链表
*/
public class DoublyLinkedListSentinel implements Iterable<Integer> {
private 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 sentinel = new Node(null, -1, null);
public DoublyLinkedListSentinel() {
sentinel.prev = sentinel;
sentinel.next = sentinel;
}
/**
* 添加到第一个
*
* @param value -待添加值
*/
public void addFirst(int value) {
Node a = sentinel;
Node b = sentinel.next;
Node added