一 特性
- 实现了Queue Deque接口
- 适合删除操作
- 可以包含重复的元素
- 非线程安全的
-
二 属性
```java 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;
<a name="mOAnz"></a>
# 三 重要方法
- 没啥重要方法,Node的结构如下 是双向链表节点
```java
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;
}
}
四 添加
/**
* Links e as last element.
* 添加时尾结点添加进入
*/
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++;
}
五 移除
public boolean remove(Object o) {
if (o == null) {
//会遍历整个链表 这里处理了值是null不能equals的情况
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;
}