双向链表(数据结构与算法)

代码:

复制代码
import java.util.Iterator;

public class DoublyLinkedListSentinel implements Iterable<Integer>{
    private Node head;//头哨兵
    private Node tail;//尾哨兵
    static class Node{
        Node prev;
        int value;
        Node next;
        Node(Node prev, int value, Node next){
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    public void DoublyLinkedListSentinel() {
        head = new Node(null, 666, null);
        tail = new Node(null, 888, null);
        head.next = tail;
        tail.prev = head;
    }

    //根据索引查找节点
    private Node findNode(int index){
        int i=-1;
        for(Node p=head;p!=tail;p=p.next,i++){
            if(i==index){
                return p;
            }
        }
        return null;
    }
    //在头部插入值
    public void addFirst(int value){
        insert(0,value);
    }
    //尾部添加
    public void addLast(int value){
        Node prev=tail.prev;
        Node added = new Node(prev, value, tail);
        prev.next=added;
        tail.prev=added;
    }

    //删除第一个节点
    public void removeFirst(){
        remove(0);
    }

    //在尾部删除
    public void removeLast(){
        Node removed = tail.prev;
        if(removed==head)//头哨兵不能删
            throw illegal(0);
        Node prev=removed.prev;
        prev.next=tail;
        tail.prev=prev;
    }
    //根据索引插入值
    public void insert(int index,int value){
        //index为0时,findNode(index-1)的值为666,不是空
        Node prev= findNode(index-1);//找到要插入索引的前一个节点
        if(prev==null) {
            throw illegal(index)  ;
        }
        Node next=prev.next;
        Node inserted=new Node(prev,value,next);
        prev.next=inserted;//指向新节点
        next.prev=inserted;
    }

    //按索引删除元素
    public void remove(int index){
        Node prev=findNode(index-1);
        //超出索引
        if(prev==null)
            throw illegal(index);

        Node removed=prev.next;
        if(removed==tail)//尾哨兵不能删
            throw illegal(index);
        Node next=removed.next;

        prev.next=next;
        next.prev=prev;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>(){
            Node p=head.next;
            @Override
            public boolean hasNext(){
                return p!=tail;
            }
            public Integer next(){
                int value=p.value;
                p=p.next;
                return value;
            }
        };
    }

    private IllegalArgumentException illegal(int index) {
        throw new IllegalArgumentException(String.format("索引[%d]不存在", index));
    }
}
相关推荐
蓝海星梦18 分钟前
GRPO 算法演进:2025 年 RL4LLM 领域 40+ 项改进工作全景解析
论文阅读·人工智能·深度学习·算法·自然语言处理·强化学习
拼好饭和她皆失19 分钟前
图论:最小生成树,二分图详细模板及讲解
c++·算法·图论
傻小胖19 分钟前
19.ETH-挖矿算法-北大肖臻老师客堂笔记
笔记·算法·区块链
郝学胜-神的一滴19 分钟前
线性判别分析(LDA)原理详解与实战应用
人工智能·python·程序人生·算法·机器学习·数据挖掘·sklearn
阿猿收手吧!19 分钟前
【C++】C++原子类型隐式转换解析
java·c++
追逐梦想的张小年20 分钟前
JUC编程02
java·idea
我是一只小小鱼~21 分钟前
JAVA 使用spring boot 搭建WebAPI项目
java·数据库·spring boot
量子炒饭大师21 分钟前
【C++入门】—— 【什么时候需要用到深拷贝】C++的类中何时需要用到深拷贝?保姆级别带你罗列所有可能!
java·c++·dubbo·深拷贝
ScilogyHunter22 分钟前
CW方程的向量形式与解析形式
算法·矩阵·控制