Version: Next

双端队列Deque

  • 双端队列是能在头尾两端,添加删除的队列
  • Double ended queue—— 简称Deque

API

方法功能
int size()元素的数量
boolean isEmpty()是否为空
void enQueueRear(E element)从队尾入队
E deQueueFront()从队头出队
void enQueueFront(E element)从队头入队
E dequeueRear()从队尾出队
E front()获取队列的头元素
E rear()获取队列的尾元素

代码

/**
* 双端队列
*/
public class MyDeque<E> {
private List<E> list = new LinkedList<>();
public int size() {
return list.size();
}
public void enQueueRear(E element) {
list.add(element);
}
public E deQueueFront() {
return list.remove(0);
}
public void enQueueFront(E element) {
list.add(0, element);
}
public E dequeueRear() {
return list.remove(list.size() - 1);
}
public E front() { // aka JDK peekFirst()
return list.get(0);
}
public E rear() { // aka JDK peekLast()
return list.get(list.size() - 1);
}
public void clear() {
list.clear();
}
public boolean isEmpty() {
return list.isEmpty();
}
}