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

代码:

复制代码
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));
    }
}
相关推荐
天天扭码3 分钟前
🔥 别再用 class 了!JS 原型链才是 YYDS
前端·javascript·面试
无心水4 分钟前
【Java面试笔记:基础】8.对比Vector、ArrayList、LinkedList有何区别?
java·笔记·面试·vector·arraylist·linkedlist
GISer_Jinger8 分钟前
📢《告别手动抓狂!Trae国际版+BrowserTools MCP 实现前端错误调试自动化》🚀
前端
前端大白话9 分钟前
震惊!90%前端工程师都踩过的坑!computed属性vs methods到底该怎么选?一文揭秘高效开发密码
前端·vue.js·设计模式
一天睡25小时9 分钟前
React与Vue表单的对比差异
前端·javascript
作曲家种太阳9 分钟前
第七章 响应式的 watch 实现【手摸手带你实现一个vue3】
前端
前端小巷子11 分钟前
深入解析 iframe
前端
WEI_Gaot12 分钟前
ES6 模板字符串
前端·javascript
前端大白话12 分钟前
前端工程师必看!手把手教你用CSS实现超丝滑的自适应视频嵌入
前端·css·前端框架
前端大白话12 分钟前
前端必看!figure标签在响应式图片排版中的王炸操作,grid/flex布局实战指南
前端·设计模式·html