diff --git a/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque.png b/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque.png new file mode 100644 index 000000000..2337972f7 Binary files /dev/null and b/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque.png differ diff --git a/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_poll_first.png b/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_poll_first.png new file mode 100644 index 000000000..db3d9a3ce Binary files /dev/null and b/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_poll_first.png differ diff --git a/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_poll_last.png b/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_poll_last.png new file mode 100644 index 000000000..c881168b5 Binary files /dev/null and b/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_poll_last.png differ diff --git a/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_push_first.png b/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_push_first.png new file mode 100644 index 000000000..d1cfaeb9b Binary files /dev/null and b/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_push_first.png differ diff --git a/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_push_last.png b/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_push_last.png new file mode 100644 index 000000000..408990870 Binary files /dev/null and b/docs/chapter_stack_and_queue/deque.assets/linkedlist_deque_push_last.png differ diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index b8d4df9ad..e5c800020 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -222,3 +222,195 @@ comments: true /* 判断双向队列是否为空 */ let isEmpty = deque.isEmpty ``` + +## 双向队列实现 + +双向队列需要一种可以在两端添加、两端删除的数据结构。与队列的实现方法类似,双向队列也可以使用双向链表和循环数组来实现。 + +### 基于双向链表的实现 + +我们将双向链表的头结点和尾结点分别看作双向队列的队首和队尾,并且实现在两端都能添加与删除结点。 + +=== "LinkedListDeque" + ![linkedlist_deque](deque.assets/linkedlist_deque.png) + +=== "pushLast()" + ![linkedlist_deque_push_last](deque.assets/linkedlist_deque_push_last.png) + +=== "pushFirst()" + ![linkedlist_deque_push_first](deque.assets/linkedlist_deque_push_first.png) + +=== "pollLast()" + ![linkedlist_deque_poll_last](deque.assets/linkedlist_deque_poll_last.png) + +=== "pollFirst()" + ![linkedlist_deque_poll_first](deque.assets/linkedlist_deque_poll_first.png) + +以下是使用双向链表实现双向队列的示例代码。 + +=== "Java" + + ```java title="linkedlist_deque.java" + /* 双向链表结点 */ + class ListNode { + int val; // 结点值 + ListNode next; // 后继结点引用(指针) + ListNode prev; // 前驱结点引用(指针) + ListNode(int val) { + this.val = val; + prev = next = null; + } + } + + /* 基于双向链表实现的双向队列 */ + class LinkedListDeque { + private ListNode front, rear; // 头结点 front ,尾结点 rear + private int size = 0; // 双向队列的长度 + + public LinkedListDeque() { + front = rear = null; + } + + /* 获取双向队列的长度 */ + public int size() { + return size; + } + + /* 判断双向队列是否为空 */ + public boolean isEmpty() { + return size() == 0; + } + + /* 入队操作 */ + private void offer(int num, boolean isFront) { + ListNode node = new ListNode(num); + // 若链表为空,则令 front, rear 都指向 node + if (isEmpty()) + front = rear = node; + // 队首入队操作 + else if (isFront) { + // 将 node 添加至链表头部 + front.prev = node; + node.next = front; + front = node; // 更新头结点 + // 队尾入队操作 + } else { + // 将 node 添加至链表尾部 + rear.next = node; + node.prev = rear; + rear = node; // 更新尾结点 + } + size++; // 更新队列长度 + } + + /* 队首入队 */ + public void offerFirst(int num) { + offer(num, true); + } + + /* 队尾入队 */ + public void offerLast(int num) { + offer(num, false); + } + + /* 出队操作 */ + private Integer poll(boolean isFront) { + // 若队列为空,直接返回 null + if (isEmpty()) + return null; + int val; + // 队首出队操作 + if (isFront) { + val = front.val; // 暂存头结点值 + // 删除头结点 + ListNode fNext = front.next; + if (fNext != null) { + fNext.prev = null; + front.next = null; + } + front = fNext; // 更新头结点 + // 队尾出队操作 + } else { + val = rear.val; // 暂存尾结点值 + // 删除尾结点 + ListNode rPrev = rear.prev; + if (rPrev != null) { + rPrev.next = null; + rear.prev = null; + } + rear = rPrev; // 更新尾结点 + } + size--; // 更新队列长度 + return val; + } + + /* 队首出队 */ + public Integer pollFirst() { + return poll(true); + } + + /* 队尾出队 */ + public Integer pollLast() { + return poll(false); + } + + /* 访问队首元素 */ + public Integer peekFirst() { + return isEmpty() ? null : front.val; + } + + /* 访问队尾元素 */ + public Integer peekLast() { + return isEmpty() ? null : rear.val; + } + + } + ``` + +=== "C++" + + ```cpp title="linkedlist_deque.cpp" + + ``` + +=== "Python" + + ```python title="linkedlist_deque.py" + + ``` + +=== "Go" + + ```go title="linkedlist_deque.go" + + ``` + +=== "JavaScript" + + ```js title="linkedlist_deque.js" + + ``` + +=== "TypeScript" + + ```typescript title="linkedlist_deque.ts" + + ``` + +=== "C" + + ```c title="linkedlist_deque.c" + + ``` + +=== "C#" + + ```csharp title="linkedlist_deque.cs" + + ``` + +=== "Swift" + + ```swift title="linkedlist_deque.swift" + + ```