1. 基本概念
2. 基于单向队列的操作
3. 基于双向队列的操作

### 正文

#### 2. 基于单向队列的操作

• 添加节点 offer()
• 删除节点
poll() remove()
• 查找元素 peek() element()

##### 1. 添加节点

``````    /**
* Adds the specified element as the tail (last element) of this list.
*
* @param e the element to add
* @return {@code true} (as specified by {@link Queue#offer})
* @since 1.5
*/
public boolean offer(E e) {
}

##### 2. 删除节点

• poll()

``````    /**
* Retrieves and removes the head (first element) of this list.
*
* @return the head of this list, or {@code null} if this list is empty
* @since 1.5
*/
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}

• remove()

``````    /**
* Retrieves and removes the head (first element) of this list.
*
* @return the head of this list
* @throws NoSuchElementException if this list is empty
* @since 1.5
*/
public E remove() {
return removeFirst();
}

##### 4. 查找元素
• peek()

``````    /**
* Retrieves, but does not remove, the head (first element) of this list.
*
* @return the head of this list, or {@code null} if this list is empty
* @since 1.5
*/
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}

• element()

``````    /**
* Retrieves, but does not remove, the head (first element) of this list.
*
* @return the head of this list
* @throws NoSuchElementException if this list is empty
* @since 1.5
*/
public E element() {
return getFirst();
}

#### 3. 基于双向队列的操作

• 添加节点 offerFirst() offerLast() push()
• 删除节点 pollFirst() pollLast() pop() removeFirstOccurrence() removeLastOccurrence()
• 查找元素 peekFirst() peekLast()
##### 1. 添加节点
• 添加节点至队首

``````    public boolean offerFirst(E e) {
return true;
}

• 添加节点至队尾

``````    public boolean offerLast(E e) {
return true;
}

``````    public void push(E e) {
}

##### 2. 删除节点
``````    public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}

``````    public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}

``````    public E pop() {
return removeFirst();
}

• 删除某个节点的元素，在它第一次出现的位置

remove()方法是从队首还是进行遍历，如果找到与指定元素相同的节点元素，就返回，所以是在元素第一次出现的位置将其删除：

``````   public boolean removeFirstOccurrence(Object o) {
return remove(o);
}

• 删除某个节点的元素，在它最后一次出现的位置

``````    public boolean removeLastOccurrence(Object o) {
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
if (x.item == null) {
return true;
}
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
if (o.equals(x.item)) {
return true;
}
}
}
return false;
}

##### 3. 查找元素
• 得到队首、队尾的节点的元素
``````    public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}

public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}