java集合之LinkedList 详解

LinkedList 继承至AbstractSequentialList 同时实现了List 等接口

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;

构造方法解释

	// 默认无参数构造
	public LinkedList() {
    	}
    // 有参构造 参数为一个 Linkedlist c 执行addAll 方法 全部加入到当前LinkedList
 	public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }
    

add方法

	//在末尾 链接元素
	 public boolean add(E e) {
        linkLast(e);
        return true;
    }
	// 在index地方插入 元素
	public void add(int index, E element) {
        checkPositionIndex(index); // 检查index合法性 index >= 0 && index <= size;
		// 如果index == size  直接在末尾插入
        if (index == size)
            linkLast(element);
        else
            linkBefore(element, node(index));
    }
    // 根据 索引 插入 一个 LindedList c
     public boolean addAll(int index, Collection<? extends E> c) {
        checkPositionIndex(index);

        Object[] a = c.toArray(); // 将C 转变成一个 Array
        int numNew = a.length; // a的长度
        if (numNew == 0)
            return false;

        Node<E> pred, succ;
        if (index == size) { // 如果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); //新建一个结点 上一个为pred 数据为e 下一个为null
            if (pred == null) //如果pred 等于null 代表 当前链表没有结点 
                first = newNode; // first 结点就等于新建立的结点
            else
                pred.next = newNode; // 链接上新节点
            pred = newNode;// 上一个等于当前
        }

        if (succ == null) {// 如果插入位置等null 直接last 就等于最后一个pred 位置了
            last = pred;
        } else { // 否则 pred的下一个等于 插入位置的结点
            pred.next = succ; 
            succ.prev = pred;
        }

        size += numNew; //链表长度更新 
        modCount++;// 链表结构修改次数
        return true;
    }
   //套娃方法
   public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }
	 //增加第一个结点
	 public void addFirst(E e) {
        linkFirst(e);
    }
    //增加最后一个结点
    public void addLast(E e) {
        linkLast(e);
    }
	//私有方法 给addFirst调用 更改头结点
	private void linkFirst(E e) {
        final Node<E> f = first;//记录first结点
        final Node<E> newNode = new Node<>(null, e, f);// 新节点 上一个为null 数据域为e 下一个为当前first
        first = newNode; // 修改first结点
        if (f == null) //如果链表没有数据 last结点也为NewNode 
            last = newNode;
        else
            f.prev = newNode; // f 为第二个结点 要和新的first结点链接起来
        size++; // 链表长度加1
        modCount++;// 结构修改次数 +1 
    }
   // 链接一个结点 add调用了 和addLast 调用
   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 clear() {
        for (Node<E> x = first; x != null; ) {// 遍历整个链表 将所有值都置为null
            Node<E> next = x.next;
            x.item = null;
            x.next = null;
            x.prev = null;
            x = next;
        }
        first = last = null;
        size = 0;
        modCount++;
    }
// clone方法
public Object clone() {
		// 调用父类的Clone 方法
        LinkedList<E> clone = superClone();
        // Clone 后的 first last 都为null size =0 修改次数也是0
        clone.first = clone.last = null;
        clone.size = 0;
        clone.modCount = 0;
		// 给Clone链表初始化 返回一个 clone Object
        for (Node<E> x = first; x != null; x = x.next)
            clone.add(x.item);

        return clone;
    }

remove方法

 	// 无参移除方法 默认移除frist结点 返回结点值
 	public E remove() {
        return removeFirst();
    }
    // 移除 index 索引位置的 结点返回 这个结点的值
    public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }
	// 移除First 结点 返回结点值
	public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f); //实现移除
    }
    // 移除last结点 返回结点值
    public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }
    //移除item 第一个等于o的值 的结点 如果o 为null 就移除 第一个item 等于null 的结点  
    // remove 成功返回Ture  失败返回 False
     public boolean remove(Object o) {
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }
    // 套娃
    public boolean removeFirstOccurrence(Object o) {
        return remove(o);
    }
    // 移除item 最后一个等于o的值 的结点 如果o 为null 就移除 最后一个item 等于null 的结点 
    public boolean removeLastOccurrence(Object o) {
        if (o == null) {
            for (Node<E> x = last; x != null; x = x.prev) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = last; x != null; x = x.prev) {
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }

一些其他方法

	//获取 index位置的值 
    public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }
    // 修改index位置的值 返回修改之前的值
    public E set(int index, E element) {
        checkElementIndex(index);
        Node<E> x = node(index);
        E oldVal = x.item;
        x.item = element;
        return oldVal;
    }
   // 获取first结点的值
    public E getFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }
   //获取last结点的值
   public E getLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return l.item;
    }
    // 从第一个开始弹出 值
      public E pop() {
        return removeFirst();
    }

unlink方法

	// 移除这个结点 x
	 E unlink(Node<E> x) {
        final E element = x.item; // 记录 被移除结点的值
        final Node<E> next = x.next; // 下一个
        final Node<E> prev = x.prev;// 上一个
 
        if (prev == null) {// 如果上一个是null 说明被移除的是first结点 使得first结点为next 
            first = next;
        } else {  // 如果上一个不是first结点 就使得 被移除结点的上一个的next 赋值为 被移除结点的下一个 使得被移除结点断开连接
            prev.next = next;
            x.prev = null; // 设置null 给jvm 回收
        }

        if (next == null) { // 如果下一个是null 说明被移除的是 last结点 直接让last 为prev 就行
            last = prev;
        } else { //和上面一样的
            next.prev = prev;
            x.next = null;
        }

        x.item = null;
        size--; // 链表长度-1
        modCount++; // 结构修改次数 +1
        return element;
    }
    // 移除first 结点
    private E unlinkFirst(Node<E> f) {
        final E element = f.item;
        final Node<E> next = f.next;
        f.item = null;
        f.next = null; // 帮助 gc 
        first = next;
        if (next == null)
            last = null;
        else
            next.prev = null;
        size--;
        modCount++;
        return element;
    }
    // 移除last 结点
    private E unlinkLast(Node<E> l) {
        final E element = l.item;
        final Node<E> prev = l.prev;
        l.item = null;
        l.prev = null; 
        last = prev;
        if (prev == null)
            first = null;
        else
            prev.next = null;
        size--;
        modCount++;
        return element;
    }
发布了18 篇原创文章 · 获赞 5 · 访问量 383

猜你喜欢

转载自blog.csdn.net/qq_41050869/article/details/104521971