美文网首页
LinkedList源码分析

LinkedList源码分析

作者: 阿桃_28e7 | 来源:发表于2018-05-22 13:03 被阅读0次

//LinkedList只有头节点和尾节点,每个节点有一个元素值,和两个节点引用

public class LinkedList

extends AbstractSequentialList

implements List, Deque, Cloneable, java.io.Serializable

{

transient int size = 0;

/**

* Pointer to first node.

* Invariant: (first == null && last == null) ||

*            (first.prev == null && first.item != null)

*/

transient Node first;

/**

* Pointer to last node.

* Invariant: (first == null && last == null) ||

*            (last.next == null && last.item != null)

*/

transient Node last;

//获取头节点的元素值

    public E getFirst() {

final Node f = first;

if (f == null)

throw new NoSuchElementException();

return f.item;

}

//获取尾节点的元素值

    public E getLast() {

final Node l = last;

if (l == null)

throw new NoSuchElementException();

return l.item;

}

//移除头节点

    public E removeFirst() {

final Node f = first;

if (f == null)

throw new NoSuchElementException();

return unlinkFirst(f);

}

//链表断开头节点:将头节点的元素值和下一个节点取出,

    //然后置空,让GC回收【不置空会浪费内存】,

    //将取出的元素值返回,将取出的节点【如果不为空,将尾节点也置空】作为新的头节点

    //更新链表大小,和链表被修改的次数

    private E unlinkFirst(Node f) {

// assert f == first && f != null;

final E element = f.item;

final Node next = f.next;

f.item = null;

f.next = null; // help GC

first = next;

if (next == null)

last = null;

else

next.prev = null;

size--;

modCount++;

return element;

}

//移除尾节点

    public E removeLast() {

final Node l = last;

if (l == null)

throw new NoSuchElementException();

return unlinkLast(l);

}

//链表断开尾节点

    private E unlinkLast(Node l) {

// assert l == last && l != null;

final E element = l.item;

final Node prev = l.prev;

l.item = null;

l.prev = null; // help GC

last = prev;

if (prev == null)

first = null;

else

prev.next = null;

size--;

modCount++;

return element;

}

//添加头节点

  public void addFirst(E e) {

linkFirst(e);

}

//链表头连接节点

  private void linkFirst(E e) {

final Node f = first;

final Node newNode = new Node<>(null, e, f);

first = newNode;

if (f == null)//原来的头指针为空【整个链表为空】

              last = newNode;//首尾指针指向新添加的节点,该节点没有prev,next

else

f.prev = newNode;

size++;

modCount++;

}

//添加尾节点

  public void addLast(E e) {

linkLast(e);

}

//链表尾连接节点

  void linkLast(E e) {

final Node l = last;

final Node newNode = new Node<>(l, e, null);

last = newNode;

if (l == null)//原来的尾指针为空【整个链表为空】

              first = newNode;//首尾指针指向新添加的节点,该节点没有prev,next

else

l.next = newNode;

size++;

modCount++;

}

//在某个节点前面添加节点

  void linkBefore(E e, Node succ) {

// assert succ != null;

final Node pred = succ.prev;

final Node newNode = new Node<>(pred, e, succ);

succ.prev = newNode;//将插入节点的后一个节点的prev,指向插入的节点

          if (pred == null)

first = newNode;//如果插入节点的前一个节点为空,即插入的节点将作为头指针

          else

pred.next = newNode;//不为空,则插入节点的前一个节点的next,指向插入的节点

          size++;

modCount++;

}

//删除某个节点

  E unlink(Node x) {

// assert x != null;

final E element = x.item;

final Node next = x.next;

final Node 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;

}

//常规添加接口:调用的是尾添加

  public boolean add(E e) {

linkLast(e);

return true;

}

//常规删除接口:非元素为空和不为空

  public boolean remove(Object o) {

if (o == null) {

for (Node x = first; x != null; x = x.next) {

if (x.item == null) {

unlink(x);

return true;

}

}

} else {

for (Node x = first; x != null; x = x.next) {

if (o.equals(x.item)) {

unlink(x);

return true;

}

}

}

return false;

}

//添加集合

  public boolean addAll(Collection c) {

return addAll(size, c);

}

//添加集合具体实现

  public boolean addAll(int index, Collection c) {

checkPositionIndex(index);

Object[] a = c.toArray();

int numNew = a.length;

if (numNew == 0)

return false;

Node pred, succ;

if (index == size) {

succ = null;

pred = last;

} else {

succ = node(index);

pred = succ.prev;

}

for (Object o : a) {

@SuppressWarnings("unchecked") E e = (E) o;

Node newNode = new Node<>(pred, e, null);

if (pred == null)

first = newNode;

else

pred.next = newNode;

pred = newNode;

}

if (succ == null) {

last = pred;

} else {

pred.next = succ;

succ.prev = pred;

}

size += numNew;

modCount++;

return true;

}

//取得某个位置的节点

  Node node(int index) {

// assert isElementIndex(index);

if (index < (size >> 1)) {

Node x = first;

for (int i = 0; i < index; i++)

x = x.next;

return x;

} else {

Node x = last;

for (int i = size - 1; i > index; i--)

x = x.prev;

return x;

}

}

//清空链表

  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 x = first; x != null; ) {

Node next = x.next;

x.item = null;

x.next = null;

x.prev = null;

x = next;

}

first = last = null;

size = 0;

modCount++;

}

//获取指定位置元素

  public E get(int index) {

checkElementIndex(index);

return node(index).item;

}

//修改指定位置的元素

  public E set(int index, E element) {

checkElementIndex(index);

Node x = node(index);

E oldVal = x.item;

x.item = element;

return oldVal;

}

//指定位置添加元素

  public void add(int index, E element) {

checkPositionIndex(index);

if (index == size)

linkLast(element);

else

linkBefore(element, node(index));//先获取到指定位置的节点,再使用linkBefore添加新节点

      }

//删除指定位置节点

  public E remove(int index) {

checkElementIndex(index);

return unlink(node(index));//想获取到指定位置的节点,在使用unlink删除节点

      }

//获取某个元素的位置

  public int indexOf(Object o) {

int index = 0;

if (o == null) {

for (Node x = first; x != null; x = x.next) {

if (x.item == null)

return index;

index++;

}

} else {

for (Node x = first; x != null; x = x.next) {

if (o.equals(x.item))

return index;

index++;

}

}

return -1;

}

//获取头指针元素

  public E peek() {

final Node f = first;

return (f == null) ? null : f.item;

}

//删除头节点

  public E poll() {

final Node f = first;

return (f == null) ? null : unlinkFirst(f);

}

//转为数组【无参】

  public Object[] toArray() {

Object[] result = new Object[size];

int i = 0;

for (Node x = first; x != null; x = x.next)

result[i++] = x.item;

return result;

}

//转为数组【传数组】

  public T[] toArray(T[] a) {

if (a.length < size)

a = (T[])java.lang.reflect.Array.newInstance(

a.getClass().getComponentType(), size);

int i = 0;

Object[] result = a;

for (Node x = first; x != null; x = x.next)

result[i++] = x.item;

if (a.length > size)

a[size] = null;

return a;

}

相关文章

网友评论

      本文标题:LinkedList源码分析

      本文链接:https://www.haomeiwen.com/subject/oisgdftx.html