LinkedList源码分析

28次阅读

共计 2513 个字符,预计需要花费 7 分钟才能阅读完成。

总览

定义

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

  • LinkedList<E>:说明它支持 泛型
  • extends AbstractSequentialList<E>

    • AbstractSequentialList 继承自 AbstractList,但 AbstractSequentialList 只支持 按次序 访问,而不像 AbstractList 那样支持随机访问。这是 LinkedList 随机访问效率低的原因之一。
  • implements

    • List<E>:说明它支持集合的一般操作。
    • Deque<E>:Deque,Double ended queue,双端队列。LinkedList 可用作队列或双端队列就是因为实现了它。
    • Cloneable:表明其可以调用 clone()方法来返回实例的 field-for-field 拷贝。
    • java.io.Serializable:表明该类是可以 序列化 的。

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

参考资料:
https://segmentfault.com/a/11…

正文完
 0

LinkedList源码分析

28次阅读

共计 2036 个字符,预计需要花费 6 分钟才能阅读完成。

一、属性及获取属性:
1、size
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;
获取
public int size() {
return size;
}
二、构造函数
//Constructs an empty list
public LinkedList() {
}

public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
三、类
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;
}
}
四、方法
1、Node
Node<E> node(int index) {
// assert isElementIndex(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;
}
}
*、linkFirst
/**
* 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++;
}

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++;
}
*、add、addFirst
public void addFirst(E e) {
linkFirst(e);
}
public boolean add(E e) {
linkLast(e);
return true;
}
linkLast
2、set
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
3、get
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
*、clear
public void clear() {
// Clearing all of the links between nodes is “unnecessary”, but:
// – helps a generational GC if the discarded nodes inhabit
// more than one generation
// – is sure to free memory even if there is a reachable Iterator
for (Node<E> x = first; x != null;) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
*、Push Pop
public void push(E e) {
addFirst(e);
}
public E pop() {
return removeFirst();
}
*、node(int index)
Node<E> node(int index) {
// assert isElementIndex(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;
}
}

正文完
 0