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

代码:

复制代码
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));
    }
}
相关推荐
pobu1681 分钟前
aksk前端签名实现
java·前端·javascript
森焱森3 分钟前
单片机中 main() 函数无 while 循环的后果及应对策略
c语言·单片机·算法·架构·无人机
烛阴7 分钟前
带参数的Python装饰器原来这么简单,5分钟彻底掌握!
前端·python
0wioiw012 分钟前
Flutter基础(前端教程⑤-组件重叠)
开发语言·前端·javascript
平和男人杨争争21 分钟前
机器学习12——支持向量机中
算法·机器学习·支持向量机
冰天糖葫芦25 分钟前
VUE实现数字翻牌效果
前端·javascript·vue.js
南岸月明34 分钟前
我与技术无缘,只想副业搞钱
前端
一个天蝎座 白勺 程序猿34 分钟前
飞算JavaAI进阶:重塑Java开发范式的AI革命
java·开发语言·人工智能
前端 贾公子37 分钟前
tailwindCSS === 使用插件自动类名排序
java·开发语言