您的位置:首页 > 编程语言 > Java开发

LinkedList简介

2016-06-14 20:32 253 查看

LinkedList简介

LinkedList简介

LinkedList除了可以当做链表来操作外,它还可以当做栈、队列和双端队列来使用。

LinkedList同样是非线程安全的,只在单线程下适合使用。

LinkedList实现了Serializable接口,因此它支持序列化,能够通过序列化传输,实现了Cloneable接口,能被克隆。

LinkedList源码剖析

<span style="font-family:FangSong_GB2312;font-size:18px;">public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
// LinkedList中元素个数
transient int size = 0;

//链表的头节点
transient Node<E> first;
//链表的尾节点
transient Node<E> last;

// 默认构造函数
public LinkedList() {
}

// 包含“集合”的构造函数:创建一个包含“集合”的LinkedList
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}

//把e链接为链表的头节点
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++;
}

//把e链接为链表的尾节点
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++;
}

//在指定节点前插入e
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
final Node<E> pred = succ.prev;
final Node<E> newNode = new Node<>(pred, e, succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}

//删除头部元素f
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
final E element = f.item;
final Node<E> 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;
}

//删除尾部元素l
private E unlinkLast(Node<E> l) {
// assert l == last && l != null;
final E element = l.item;
final Node<E> 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;
}

//删除节点x
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;
}

// 返回第一个节点
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}

// 返回最后一个节点
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}

// 删除头部节点
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}

// 删除尾部节点
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
//在头部添加“e”
public void addFirst(E e) {
linkFirst(e);
}

//在尾部添加“e”
public void addLast(E e) {
linkLast(e);
}

//判断是否包含“o”
public boolean contains(Object o) {
return indexOf(o) != -1;
}

//返回元素个数
public int size() {
return size;
}

//在尾部添加“e”
public boolean add(E e) {
linkLast(e);
return true;
}

// 删除节点"o"
public boolean remove(Object o) {
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;
}

// 将“集合(c)”添加到链表中。
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}

// 从链表的index开始,将“集合(c)”添加到链表中。
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);

Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;

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

// 清空链表
public void clear() {
// 从表头开始,逐个向后遍历;对遍历到的节点执行一下操作:
// (01) 设置前一个节点为null
// (02) 设置当前节点的内容为null
// (03) 设置后一个节点为“新的当前节点”
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++;
}

// Positional Access Operations

// 返回LinkedList指定位置的元素
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}

// 设置index位置对应的节点的值为element
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}

// 在index前添加节点,且节点的值为element
public void add(int index, E element) {
checkPositionIndex(index);

if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}

// 删除index位置的节点
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}

//判断index位置是否存在节点
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}

//判断index位置是否存在节点以及是否可以在该位置添加节点
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}

/**
* Constructs an IndexOutOfBoundsException detail message.
* Of the many possible refactorings of the error handling code,
* this "outlining" performs best with both server and client VMs.
*/
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
//检查是否越界
private void checkElementIndex(int index) {
if (!isElementIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
//检查是否越界
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

/**
* Returns the (non-null) Node at the specified element 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;
}
}

// Search Operations

// 从前向后查找,返回“值为对象(o)的节点对应的索引”
// 不存在就返回-1
public int indexOf(Object o) {
int index = 0;
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null)
return index;
index++;
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item))
return index;
index++;
}
}
return -1;
}

// 从后向前查找,返回“值为对象(o)的节点对应的索引”
// 不存在就返回-1
public int lastIndexOf(Object o) {
int index = size;
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (x.item == null)
return index;
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (o.equals(x.item))
return index;
}
}
return -1;
}

// Queue operations.

// 获取第一个链表节点的值,判断是否为空,不为空则返回节点对应的值。
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}

//返回第一个节点的值。
public E element() {
return getFirst();
}

//删除链表的首节点;如果节点不为空就调用unlinkFirst(Node)方法,做的事情很简单,
//删除首节点,垃圾回收还有就是更新链表长度。
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}

public E remove() {
return removeFirst();
}

//将元素添加到链表的末尾;
public boolean offer(E e) {
return add(e);
}

//将元素添加到元素首部
public boolean offerFirst(E e) {
addFirst(e);
return true;
}

//将元素添加到链表的末尾;
public boolean offerLast(E e) {
addLast(e);
return true;
}

// 返回第一个节点  若LinkedList的大小为0,则返回null
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}

// 返回最后一个节点,若LinkedList的大小为0,则返回null
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}

// 删除并返回第一个节点,若LinkedList的大小为0,则返回null
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}

// 删除并返回最后一个节点
// 若LinkedList的大小为0,则返回null
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}

//将元素插入到头部,
public void push(E e) {
addFirst(e);
}

//将第一个元素删除,头部节点,
public E pop() {
return removeFirst();
}

// 从LinkedList开始向后查找,删除第一个值为元素(o)的节点
public boolean removeFirstOccurrence(Object o) {
return remove(o);
}

// 从LinkedList末尾向前查找,删除第一个值为元素(o)的节点
public boolean removeLastOccurrence(Object o) {
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}

// 返回“index到末尾的全部节点”对应的ListIterator对象(List迭代器)
public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
return new ListItr(index);
}
</span>


参考资料:http://blog.csdn.net/ns_code/article/details/35787253以及jdk 1.8
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息