Unify the function naming of

queue from `offer()` to `push()`
This commit is contained in:
Yudong Jin
2023-02-02 01:43:01 +08:00
parent a0ee691475
commit 7d14c9440e
29 changed files with 184 additions and 189 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 65 KiB

View File

@ -12,16 +12,16 @@ comments: true
## 5.3.1. 双向队列常用操作
双向队列的常用操作见下表方法命名以 Java 为例)
双向队列的常用操作见下表方法名需根据特定语言来确定
<p align="center"> Table. 双向队列的常用操作 </p>
<div class="center-table" markdown>
| 方法 | 描述 | 时间复杂度 |
| 方法 | 描述 | 时间复杂度 |
| ------------ | ---------------- | ---------- |
| offerFirst() | 将元素添加至队首 | $O(1)$ |
| offerLast() | 将元素添加至队尾 | $O(1)$ |
| pushFirst() | 将元素添加至队首 | $O(1)$ |
| pushLast() | 将元素添加至队尾 | $O(1)$ |
| pollFirst() | 删除队首元素 | $O(1)$ |
| pollLast() | 删除队尾元素 | $O(1)$ |
| peekFirst() | 访问队首元素 | $O(1)$ |
@ -288,7 +288,7 @@ comments: true
}
/* 入队操作 */
private void offer(int num, boolean isFront) {
private void push(int num, boolean isFront) {
ListNode node = new ListNode(num);
// 若链表为空,则令 front, rear 都指向 node
if (isEmpty())
@ -310,13 +310,13 @@ comments: true
}
/* 队首入队 */
public void offerFirst(int num) {
offer(num, true);
public void pushFirst(int num) {
push(num, true);
}
/* 队尾入队 */
public void offerLast(int num) {
offer(num, false);
public void pushLast(int num) {
push(num, false);
}
/* 出队操作 */

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB

After

Width:  |  Height:  |  Size: 58 KiB

View File

@ -14,19 +14,19 @@ comments: true
## 5.2.1. 队列常用操作
队列的常用操作见下表方法命名以 Java 为例)
队列的常用操作见下表方法名需根据特定语言来确定
<p align="center"> Table. 队列的常用操作 </p>
<div class="center-table" markdown>
| 方法 | 描述 | 时间复杂度 |
| --------- | ---------------------------- | ---------- |
| offer() | 元素入队,即将元素添加至队尾 | $O(1)$ |
| poll() | 队首元素出队 | $O(1)$ |
| front() | 访问队首元素 | $O(1)$ |
| size() | 获取队列的长度 | $O(1)$ |
| isEmpty() | 判断队列是否为空 | $O(1)$ |
| 方法 | 描述 | 时间复杂度 |
| --------- | -------------------------- | -------- |
| push() | 元素入队,即将元素添加至队尾 | $O(1)$ |
| poll() | 队首元素出队 | $O(1)$ |
| front() | 访问队首元素 | $O(1)$ |
| size() | 获取队列的长度 | $O(1)$ |
| isEmpty() | 判断队列是否为空 | $O(1)$ |
</div>
@ -302,7 +302,7 @@ comments: true
return size() == 0;
}
/* 入队 */
public void offer(int num) {
public void push(int num) {
// 尾结点后添加 num
ListNode node = new ListNode(num);
// 如果队列为空,则令头、尾结点都指向该结点
@ -361,7 +361,7 @@ comments: true
return queSize == 0;
}
/* 入队 */
void offer(int num) {
void push(int num) {
// 尾结点后添加 num
ListNode* node = new ListNode(num);
// 如果队列为空,则令头、尾结点都指向该结点
@ -459,8 +459,8 @@ comments: true
}
}
// offer 入队
func (s *linkedListQueue) offer(value any) {
// push 入队
func (s *linkedListQueue) push(value any) {
s.data.PushBack(value)
}
@ -515,7 +515,7 @@ comments: true
return this.size === 0;
}
/* 入队 */
offer(num) {
push(num) {
// 尾结点后添加 num
const node = new ListNode(num);
// 如果队列为空,则令头、尾结点都指向该结点
@ -567,7 +567,7 @@ comments: true
return this.size === 0;
}
/* 入队 */
offer(num: number): void {
push(num: number): void {
// 尾结点后添加 num
const node = new ListNode(num);
// 如果队列为空,则令头、尾结点都指向该结点
@ -630,7 +630,7 @@ comments: true
return size() == 0;
}
/* 入队 */
public void offer(int num)
public void push(int num)
{
// 尾结点后添加 num
ListNode node = new ListNode(num);
@ -689,7 +689,7 @@ comments: true
}
/* 入队 */
func offer(num: Int) {
func push(num: Int) {
// 尾结点后添加 num
let node = ListNode(x: num)
// 如果队列为空,则令头、尾结点都指向该结点
@ -746,7 +746,7 @@ comments: true
细心的同学可能会发现一个问题,即在入队与出队的过程中,两个指针都在向后移动,**在到达尾部后则无法继续移动了**。
为了解决此问题,我们可以采取一个取巧方案,**即将数组看作是“环形”的**。具体做法是规定指针越过数组尾部后,再次回到头部接续遍历,这样相当于使数组“首尾相连”了。在环形数组的设定下,获取长度 `size()` 、入队 `offer()` 、出队 `poll()` 方法都需要做相应的取余操作处理,使得当尾指针绕回数组头部时,仍然可以正确处理操作。
为了解决此问题,我们可以采取一个取巧方案,**即将数组看作是“环形”的**。具体做法是规定指针越过数组尾部后,再次回到头部接续遍历,这样相当于使数组“首尾相连”了。在环形数组的设定下,获取长度 `size()` 、入队 `push()` 、出队 `poll()` 方法都需要做相应的取余操作处理,使得当尾指针绕回数组头部时,仍然可以正确处理操作。
=== "Java"
@ -778,7 +778,7 @@ comments: true
}
/* 入队 */
public void offer(int num) {
public void push(int num) {
if (queSize == capacity()) {
System.out.println("队列已满");
return;
@ -848,7 +848,7 @@ comments: true
}
/* 入队 */
void offer(int num) {
void push(int num) {
if (queSize == queCapacity) {
cout << "队列已满" << endl;
return;
@ -955,8 +955,8 @@ comments: true
return q.queSize == 0
}
// offer 入队
func (q *arrayQueue) offer(num int) {
// push 入队
func (q *arrayQueue) push(num int) {
// 当 rear == queCapacity 表示队列已满
if q.queSize == q.queCapacity {
return
@ -1026,7 +1026,7 @@ comments: true
}
/* 入队 */
offer(num) {
push(num) {
if (this.size == this.capacity)
throw new Error("队列已满");
// 计算尾指针,指向队尾索引 + 1
@ -1085,7 +1085,7 @@ comments: true
}
/* 入队 */
offer(num: number): void {
push(num: number): void {
if (this.size == this.capacity)
throw new Error("队列已满");
// 计算尾指针,指向队尾索引 + 1
@ -1155,7 +1155,7 @@ comments: true
}
/* 入队 */
public void offer(int num)
public void push(int num)
{
if (queSize == capacity())
{
@ -1220,7 +1220,7 @@ comments: true
}
/* 入队 */
func offer(num: Int) {
func push(num: Int) {
if size() == capacity() {
print("队列已满")
return