数据存储核心之LinkedList源码探究

类图结构

在这里插入图片描述
        LinkedList是通过双向链表去实现的,他的数据结构具有双向链表结构的优缺点
        既然是双向链表,那么它的顺序访问会非常高效,而随机访问效率比较低。
        它包含一个非常重要的私有的静态内部类:Node。

private static class Node<E> {
    E item; // 存储的元素
    Node<E> next; // 下一个Node节点
    Node<E> prev; // 前一个Node节点

    Node(Node<E> prev, E element, Node<E> next) {
        this.item = element;
        this.next = next;
        this.prev = prev;
    }
}

在这里插入图片描述

重要的属性
transient int size = 0; // 链表的长度

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

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

构造方法
LinkedList linkedlist = new LinkedList();
 	 /**
     * Constructs an empty list.
     * 默认为空
     */
    public LinkedList() {
    }

添加数据方式1

        从头部添加

linkedlist.push(1);

        源码

/**
     * Pushes an element onto the stack represented by this list.  In other
     * words, inserts the element at the front of this list.
     *
     * <p>This method is equivalent to {@link #addFirst}.
     *
     * @param e the element to push
     * @since 1.6
     */
    public void push(E e) {
        addFirst(e);
    }
/**
     * Inserts the specified element at the beginning of this list.
     *
     * @param e the element to add
     */
    public void addFirst(E e) {
        linkFirst(e);
    }
 /**
     * Links e as first element.
     */
    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++;
    }

添加数据方式2

        从尾部添加

linkedlist.add(2);

        源码

/**
     * Appends the specified element to the end of this list.
     *
     * <p>This method is equivalent to {@link #addLast}.
     *
     * @param e element to be appended to this list
     * @return {@code true} (as specified by {@link Collection#add})
     */
    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++;
    }

        由于LinkedList实现了List的接口,所有必然具备List的特性.接下来看List接口中定义的一个方法get(int index)和set(int index,E e);

get(index)
public E get(int index) {
    checkElementIndex(index); // 检查下标是否合法
    return node(index).item;
}

Node<E> node(int index) {
    // assert isElementIndex(index);
    // 判断index是否小于size的一半
    if (index < (size >> 1)) {
        Node<E> x = first;
// 从头开始遍历
        for (int i = 0; i < index; i++)
            x = x.next;
        return x;
    } else {
        Node<E> x = last;
// 从尾部开始遍历
        for (int i = size - 1; i > index; i--)
            x = x.prev;
        return x;
    }
}

        本质还是遍历链表中的数据

set(int index,E e)
public E set(int index, E element) {
    checkElementIndex(index); // 检查下标是否越界
    Node<E> x = node(index);  // 根据下标获取对应的Node节点 
    E oldVal = x.item;  // 记录原来的值
    x.item = element; // 修改
    return oldVal; // 返回原来的值
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值