refactor: Replace poll with pop in Queue and Deque (#415)

This commit is contained in:
Yudong Jin
2023-03-13 21:58:21 +08:00
committed by GitHub
parent 2d17ee8e92
commit 8aebbaad21
77 changed files with 261 additions and 261 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 71 KiB

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

After

Width:  |  Height:  |  Size: 65 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 66 KiB

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 63 KiB

After

Width:  |  Height:  |  Size: 63 KiB

View File

@@ -6,18 +6,18 @@
## 双向队列常用操作
双向队列的常用操作见下表,方法名需根据语言来确定,此处以 Java 为例
双向队列的常用操作见下表,方法名需根据语言来确定。
<div class="center-table" markdown>
| 方法名 | 描述 | 时间复杂度 |
| ------------ | ---------------- | ---------- |
| pushFirst() | 将元素添加至队首 | $O(1)$ |
| pushLast() | 将元素添加至队尾 | $O(1)$ |
| pollFirst() | 删除队首元素 | $O(1)$ |
| pollLast() | 删除队尾元素 | $O(1)$ |
| peekFirst() | 访问队首元素 | $O(1)$ |
| peekLast() | 访问队尾元素 | $O(1)$ |
| 方法名 | 描述 | 时间复杂度 |
| ----------- | -------------- | ---------- |
| pushFirst() | 将元素添加至队首 | $O(1)$ |
| pushLast() | 将元素添加至队尾 | $O(1)$ |
| popFirst() | 删除队首元素 | $O(1)$ |
| popLast() | 删除队尾元素 | $O(1)$ |
| peekFirst() | 访问队首元素 | $O(1)$ |
| peekLast() | 访问队尾元素 | $O(1)$ |
</div>
@@ -41,8 +41,8 @@
int peekLast = deque.peekLast(); // 队尾元素
/* 元素出队 */
int pollFirst = deque.pollFirst(); // 队首元素出队
int pollLast = deque.pollLast(); // 队尾元素出队
int popFirst = deque.pollFirst(); // 队首元素出队
int popLast = deque.pollLast(); // 队尾元素出队
/* 获取双向队列的长度 */
int size = deque.size();
@@ -266,9 +266,9 @@
let peekLast = deque.last! // 队尾元素
/* 元素出队 */
// 使用 Array 模拟时 pollFirst 的复杂度为 O(n)
let pollFirst = deque.removeFirst() // 队首元素出队
let pollLast = deque.removeLast() // 队尾元素出队
// 使用 Array 模拟时 popFirst 的复杂度为 O(n)
let popFirst = deque.removeFirst() // 队首元素出队
let popLast = deque.removeLast() // 队尾元素出队
/* 获取双向队列的长度 */
let size = deque.count
@@ -304,11 +304,11 @@
=== "pushFirst()"
![linkedlist_deque_push_first](deque.assets/linkedlist_deque_push_first.png)
=== "pollLast()"
![linkedlist_deque_poll_last](deque.assets/linkedlist_deque_poll_last.png)
=== "popLast()"
![linkedlist_deque_pop_last](deque.assets/linkedlist_deque_pop_last.png)
=== "pollFirst()"
![linkedlist_deque_poll_first](deque.assets/linkedlist_deque_poll_first.png)
=== "popFirst()"
![linkedlist_deque_pop_first](deque.assets/linkedlist_deque_pop_first.png)
以下是具体实现代码。
@@ -403,11 +403,11 @@
=== "pushFirst()"
![array_deque_push_first](deque.assets/array_deque_push_first.png)
=== "pollLast()"
![array_deque_poll_last](deque.assets/array_deque_poll_last.png)
=== "popLast()"
![array_deque_pop_last](deque.assets/array_deque_pop_last.png)
=== "pollFirst()"
![array_deque_poll_first](deque.assets/array_deque_poll_first.png)
=== "popFirst()"
![array_deque_pop_first](deque.assets/array_deque_pop_first.png)
以下是具体实现代码。

Binary file not shown.

Before

Width:  |  Height:  |  Size: 72 KiB

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 70 KiB

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 53 KiB

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 56 KiB

View File

@@ -8,15 +8,15 @@
## 队列常用操作
队列的常用操作见下表,方法名需根据语言来确定,此处以 Java 为例
队列的常用操作见下表。需要注意,不同编程语言的方法名是不同的,在这里我们采用与栈相同的方法命名
<div class="center-table" markdown>
| 方法名 | 描述 | 时间复杂度 |
| --------- | -------------------------- | -------- |
| push() | 元素入队,即将元素添加至队尾 | $O(1)$ |
| poll() | 队首元素出队 | $O(1)$ |
| peek() | 访问队首元素 | $O(1)$ |
| pop() | 队首元素出队 | $O(1)$ |
| peek() | 访问队首元素 | $O(1)$ |
</div>
@@ -39,7 +39,7 @@
int peek = queue.peek();
/* 元素出队 */
int poll = queue.poll();
int pop = queue.poll();
/* 获取队列的长度 */
int size = queue.size();
@@ -120,8 +120,8 @@
peek := queue.Front()
/* 元素出队 */
poll := queue.Front()
queue.Remove(poll)
pop := queue.Front()
queue.Remove(pop)
/* 获取队列的长度 */
size := queue.Len()
@@ -149,7 +149,7 @@
/* 元素出队 */
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
const poll = queue.shift();
const pop = queue.shift();
/* 获取队列的长度 */
const size = queue.length;
@@ -177,7 +177,7 @@
/* 元素出队 */
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
const poll = queue.shift();
const pop = queue.shift();
/* 获取队列的长度 */
const size = queue.length;
@@ -209,7 +209,7 @@
int peek = queue.Peek();
/* 元素出队 */
int poll = queue.Dequeue();
int pop = queue.Dequeue();
/* 获取队列的长度 */
int size = queue.Count();
@@ -236,7 +236,7 @@
let peek = queue.first!
/* 元素出队 */
// 使用 Array 模拟时 poll 的复杂度为 O(n)
// 由于是数组,因此 removeFirst 的复杂度为 O(n)
let pool = queue.removeFirst()
/* 获取队列的长度 */
@@ -266,8 +266,8 @@
=== "push()"
![linkedlist_queue_push](queue.assets/linkedlist_queue_push.png)
=== "poll()"
![linkedlist_queue_poll](queue.assets/linkedlist_queue_poll.png)
=== "pop()"
![linkedlist_queue_pop](queue.assets/linkedlist_queue_pop.png)
以下是使用链表实现队列的示例代码。
@@ -350,8 +350,8 @@
=== "push()"
![array_queue_push](queue.assets/array_queue_push.png)
=== "poll()"
![array_queue_poll](queue.assets/array_queue_poll.png)
=== "pop()"
![array_queue_pop](queue.assets/array_queue_pop.png)
细心的同学可能会发现一个问题:在不断入队与出队的过程中,`front` 和 `rear` 都在向右移动,**在到达数组尾部后就无法继续移动了**。为解决此问题,**我们考虑将数组看作是首尾相接的**,这样的数组被称为「环形数组」。

View File

@@ -10,7 +10,7 @@
## 栈常用操作
栈的常用操作见下表,方法名需根据语言来确定,此处以 Java 为例。
栈的常用操作见下表,方法名需根据编程语言来确定,此处我们以常见的 `push` , `pop` , `peek` 为例。
<div class="center-table" markdown>