LinkedList简析(1)

概述

LinkedList 也是 List 接口的实现类,与 ArrayList 不同之处是采用的存储结构不同,ArrayList 的数据结构为线性表,而 LinkedList 数据结构是链表。链表数据结构的特点是每个元素分配的空间不必连续,插入和删除元素时速度非常快,但访问元素的速度较慢
LinkedList结构特征

继承实现

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

源码可知,LinkedList 继承了一个类(AbstractSequenceList 类)
实现4个接口(List,Deque,Cloneable,java.io.Serializable接口)

  • AbstractSequenceList : 这个接口是 List 一系列子类接口的核心接口,以求最大限度的减少实现 List 接口的工作量
  • Deque : 定义了双端的操作,支持两端插入和移除元素

数据结构

LinkedList是基于链表结构实现的,所以在类中包含了first和last两个指针(Node)。Node中包含了上一个节点和下一个节点的引用,这样就构成了两个的链表。就像是电视剧里的秘密组织,每一个对象都只知道也只能联系到自己的上线和下线

 	// size 表示当前链表的元素个数
 	transient int size = 0;

    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
     // first 指向链表的头指针
    transient Node<E> first;

    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
     // last 指向链表的尾指针
    transient Node<E> last;

Node用来构造链表的每一个节点,item表示元素,next指向下一个节点,prev指向上一个节点,就如同开篇的结构特征图

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;
        }
}

LinkedList 有两个构造函数,一个是空构造函数,不添加任何元素,一种是创建的时候就接收一个Collection集合

  • 空构造函数
/**
     * Constructs an empty list.
     */
    public LinkedList() {
    }
  • 创建一个包含指定元素的构造函数
  	 /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param  c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值