LinkedList

LinkedList

列表和双向链表的结合,允许值为null,检索时,从头或从尾开始,以更接近的索引为准.

一、LinkedList的定义

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
    
}

二、LinkedList的属性

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
    
    // 链表的大小
    transient int size = 0;
    
    // 第一个节点
    transient Node<E> first;
    
    // 最后一个节点
    transient Node<E> last;
}

三、LinkedList的构造器

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
    
    public LinkedList() {
    }
    
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }
}

四、LinkedList的主要方法

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
    
    /**
    * 添加元素
    */
    public boolean add(E e) {
        linkLast(e);
        return true;
    }
    
    /**
    * 在链表尾部添加元素
    */
    void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }
    
    /**
    * 在链表指定位置添加元素
    */  
    public void add(int index, E element) {
        checkPositionIndex(index);

        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }
    
    /**
    * 在链表指定位置添加元素
    */
    void linkBefore(E e, Node<E> succ) {
        // assert succ != null;
        final Node<E> pred = succ.prev;
        final Node<E> newNode = new Node<>(pred, e, succ);
        succ.prev = newNode;
        if (pred == null)
            first = newNode;
        else
            pred.next = newNode;
        size++;
        modCount++;
    }
    
    /**
    * 在链表头添加元素
    */
    public void addFirst(E e) {
        linkFirst(e);
    }
    private void linkFirst(E e) {
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode;
        size++;
        modCount++;
    }

    /**
    * 在链表尾添加元素
    */
    public void addLast(E e) {
        linkLast(e);
    }
    
    /**
    * 在链表尾部添加集合
    */
    public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }
    
    /**
    * 在链表指定位置添加集合
    */  
    public boolean addAll(int index, Collection<? extends E> c) {
        // 校验索引
        checkPositionIndex(index);

        Object[] a = c.toArray();
        int numNew = a.length;
        if (numNew == 0)
            return false;
            
        // pred 伪头节点,succ 伪尾节点
        Node<E> pred, succ;
        if (index == size) {
            // 在最后添加
            succ = null;
            pred = last;
        } else {
            // 在中间添加
            succ = node(index);
            pred = succ.prev;
        }

        for (Object o : a) {
            @SuppressWarnings("unchecked") E e = (E) o;
            Node<E> newNode = new Node<>(pred, e, null);
            if (pred == null)
                first = newNode;
            else
                pred.next = newNode;
            pred = newNode;
        }

        if (succ == null) {
            last = pred;
        } else {
            pred.next = succ;
            succ.prev = pred;
        }

        size += numNew;
        modCount++;
        return true;
    }
    
    /**
    * 删除第一个元素
    */
    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }
    
    /**
    * 删除最后一个元素
    */
    public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }
    
    /**
    * 删除指定元素
    */
    public boolean remove(Object o) {
        
    }
    
    /**
    * 删除元素
    */
    E unlink(Node<E> x) {
        
    }
    
    // Queue相关方法
    
    // 返回头元素
    public E peek() {}
    public E peekFirst() {}
    public E peekLast() {}
    
    // 返回头元素,为null时抛出异常
    public E element() {}
    
    // 删除头元素
    public E poll() {}
    public E pollFirst() {}
    public E pollLast() {}
    
    // 删除头元素,为null时会抛出异常
    public E remove() {}
    
    // 增加元素
    public boolean offer(E e) {}
    public boolean offerFirst(E e) {}
    public boolean offerLast(E e) {}
    
    // 入栈
    public void push(E e) {}
    
    // 出栈
    public E pop() {}
}

五、Node结构体

private static class Node<E> {
    E item;
    Node<E> next;
    Node<E> prev;

    Node(Node<E> prev, E element, Node<E> next) {
        this.item = element;
        this.next = next;
        this.prev = prev;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值