×

# 算法练习(40): 深入浅出双端队列Deque(1.3.33)

• 双端队列Deque

### 题目

1.3.33 Deque。一个双向队列(或者称为deque)和栈或队列类似，但它同时支持在两端添加或删除元素。Deque能够存储一组元素并支持如下API。

API for a generic double-ended queue

1.3.33 Deque. A double-ended queue or deque (pronounced “deck”) is like a stack or a queue but supports adding and removing items at both ends. A deque stores a collec- tion of items and supports the following API: Write a class Deque that uses a doubly-linked list to implement this API and a class ResizingArrayDeque that uses a resizing array.

### 分析

• Java中的双端队列类
Deque.java：Java的Util库自带Deque类，因此可以直接new一个Deque对象，大家可以找相关教程查看Deque的用法
• Objective-C中的双端队列
CFArray.C：是CoreFoundation框架中的数组实现类，它就通过Deque实现的。关于CoreFoundation框架就不多说了，iOS开发的小伙伴应该都知道。

``````public class ResizingArrayDeque1<Item> implements Iterable<Item> {

private Item[] a;
private int N;

public boolean isEmpty() {
return N == 0;
}
//构造函数
public ResizingArrayDeque1() {
a = (Item[]) (new Object[1]);
}
//大小
public int size() {
return N;
}

public void pushLeft(Item item) {
Item[] temp = (Item[]) (new Object[N + 1]);
temp[0] = item;
for (int i = 0; i < N; i++) {
temp[i + 1] = a[i];
}
a = temp;
N++;
}

private void resize(int max) {
Item[] temp = (Item[]) (new Object[max]);
for (int i = 0; i < N; i++) {
temp[i] = a[i];
}
a = temp;
}

public void pushRight(Item item) {
if (N == a.length) {
resize(N * 2);
}
a[N++] = item;
}

public Item popLeft() {
Item[] temp = (Item[]) (new Object[N + 1]);
Item popedItem = a[0];
for (int i = 0; i < N; i++) {
temp[i] = a[i + 1];
}
a = temp;
N--;
return popedItem;
}

public Item popRight() {
Item popedItem = a[--N];
a[N] = null;
if (a.length == 4 * N) {
resize(a.length / 2);
}
return popedItem;
}

public Iterator<Item> iterator() {
return new DequeIterator();
}

private class DequeIterator implements Iterator<Item> {
int index = 0;

public boolean hasNext() {
return index != N;
}

public Item next() {
return a[index++];
}

public void remove() {
}
}
}
``````

``````public void pushLeft(Item item) {
//pushLeft的时候每次都新建对象，耗时
Item[] temp = (Item[]) (new Object[N + 1]);
temp[0] = item;
for (int i = 0; i < N; i++) {
temp[i + 1] = a[i];
}
a = temp;
N++;
}
``````

#### 下面开始我们的主题，如何更好的创建一个双端队列。

1.init

Deque init

2.pushLeft

``````public void pushLeft(Item item) {
}
``````

Deque pushLeft

2.pushRight

``````public void pushRight(Item item) {
a[tail++] = item;
}
``````
Deque pushRight

3.popLeft

``````public Item popLeft() {
}
``````

Deque popLeft

4.Resize

``````private void resize(int max) {
if (max < 3) {
max = 3;
}
Item tmp[] = (Item[]) new Object[max];
int j = max / 3;

for (int i = head; i < tail; i++) {
tmp[j++] = a[i];
}
a = tmp;
head = max / 3;
tail = j;
}
``````

(1)什么时候resize
(2)resize的参数怎么传

(2)按我们以前的经验应该是传4，因为初始数组大小是2，每次调整都是2倍2倍调整，假设我们大小调整为4，那就相当于给数组的头部和尾部各加个位置，其实也是可以的，但两边各家一个元素其实幅度还是太小，因此我们这里选择，给上下各加当前大小的两倍。

### 答案

``````public class ResizingArrayDeque2<Item> implements Iterable<Item> {

private Item[] a;

private int head = 1;
private int tail = 1;

public boolean isEmpty() {
return head == tail;
}

public ResizingArrayDeque2() {
a = (Item[]) (new Object[2]);
}

public int size() {
return tail - head;
}

public void pushLeft(Item item) {
if (head == 0) {
resize(3 * size());
}
}

private void resize(int max) {
if (max < 3) {
max = 3;
}
Item tmp[] = (Item[]) new Object[max];
int j = max / 3;

for (int i = head; i < tail; i++) {
tmp[j++] = a[i];
}
a = tmp;
head = max / 3;
tail = j;
}

public void pushRight(Item item) {
if (tail == a.length) {
resize(3 * size());
}
a[tail++] = item;
}

public Item popLeft() {
if (isEmpty()) {
return null;
}
if (size() * 6 < a.length) {
resize(size() * 3);
}
}

public Item popRight() {
if (isEmpty()) {
return null;
}
if (size() * 6 < a.length) {
resize(size() * 3);
}
return a[--tail];
}

public Iterator<Item> iterator() {
return new DequeIterator();
}

private class DequeIterator implements Iterator<Item> {

private int index = head;

public boolean hasNext() {
return index < tail;
}

public Item next() {
Item x = a[index++];
return x;
}

public void remove() {

}

}

public static void main(String[] args) {
ResizingArrayDeque2<String> deque = new ResizingArrayDeque2<String>(10);
deque.pushRight("我");
deque.pushRight("的");
deque.pushRight("名字");
deque.pushRight("叫顶级程序员不穿女装");
deque.pushRight("微博:https://m.weibo.cn/p/1005056186766482");

for (String string : deque) {
System.out.println(string);
}
System.out.println("===========================");

ResizingArrayDeque2<String> deque1 = new ResizingArrayDeque2<String>(10);
deque1.pushLeft("我");
deque1.pushLeft("的");
deque1.pushLeft("名字");
deque1.pushLeft("叫顶级程序员不穿女装");
deque1.pushLeft("微博:https://m.weibo.cn/p/1005056186766482");

for (String string : deque1) {
System.out.println(string);
}
System.out.println("===========================");

deque.popLeft();

for (String string : deque) {
System.out.println(string);
}

System.out.println("===========================");
deque1.popRight();

for (String string : deque1) {
System.out.println(string);
}

}

}
``````

### 代码索引

ResizingArrayDeque2.java