LinkedList源码分析

栏目: 数据库 · 发布时间: 6年前

内容简介:LinkedList底层是从结构上,我们还看到了LinkedList实现了Deque接口,因此,我们可以操作LinkedList像操作队列和栈一样

总览

LinkedList源码分析

LinkedList底层是 双向链表

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实现了Deque接口,因此,我们可以操作LinkedList像操作队列和栈一样

构造方法

  • LinkedList()
  • LinkedList(Collection<? extends E> c)
/**
     * 构造一个空链表.
     */
    public LinkedList() {
    }


    /**
     * 根据指定集合c构造linkedList。先构造一个空linkedlist,在把指定集合c中的所有元素都添加到linkedList中。
     */
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

核心方法

add(E e)

/**
     * 将元素添加到链表尾部
     */
    public boolean add(E e) {
        linkLast(e);
        return true;
    }
    
    /**
     * 在表尾插入指定元素e
     */
    void linkLast(E e) {
        final Node<E> l = last;
        //新建节点newNode,节点的前指针指向l,后指针为null
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        //如果原来的尾结点为null,更新头指针,否则使原来的尾结点l的后置指针指向新的头结点newNode
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }

remove(Object o)

/**
     * 正向遍历链表,删除出现的第一个值为指定对象的节点
     */
    public boolean remove(Object o) {
        //LinkedList允许存放Null
        //如果删除对象为null
        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;
    }
    
    /**
     * 删除指定节点,返回指定元素的值
     */
    E unlink(Node<E> x) {
        // assert x != null;
        // 保存指定节点的值
        final E element = x.item;
        //得到后继节点
        final Node<E> next = x.next;
        //得到前驱节点
        final Node<E> prev = x.prev;

        if (prev == null) {
            //如果删除的节点是头节点,令头节点指向该节点的后继节点
            first = next;
        } else {
            //将前驱节点的后继节点指向后继节点
            prev.next = next;
            x.prev = null;
        }

        if (next == null) {
            //如果删除的节点是尾节点,令尾节点指向该节点的前驱节点
            last = prev;
        } else {
            next.prev = prev;
            x.next = null;
        }

        x.item = null;
        size--;
        modCount++;
        return element;
    }

get(int index)

/**
     * 返回指定索引处的元素
     */
    public E get(int index) {
        //检查index范围是否在size之内
        checkElementIndex(index);
        //调用node(index)去找到index对应的node然后返回它的值
        return node(index).item;
    }
    
     /**
     * 返回在指定索引处的非空元素
     */
    Node<E> node(int index) {
        // 下标小于长度的一半,从头遍历,否则从尾遍历
        
        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 element)

/**
     * 替换指定索引处的元素为指定元素element
     */
    public E set(int index, E element) {
        checkElementIndex(index);
        Node<E> x = node(index);
        E oldVal = x.item;
        x.item = element;
        return oldVal;
    }

以上所述就是小编给大家介绍的《LinkedList源码分析》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Data Structures and Algorithms in Java

Data Structures and Algorithms in Java

Michael T. Goodrich、Roberto Tamassia / Wiley / 2010-01-26 / USD 177.41

* This newest edition examines fundamental data structures by following a consistent object-oriented framework that builds intuition and analysis skills of data structures and algorithms * Presents ne......一起来看看 《Data Structures and Algorithms in Java》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具