mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 04:06:28 +08:00
Unify the function naming of
queue from `offer()` to `push()`
This commit is contained in:
parent
a0ee691475
commit
7d14c9440e
29 changed files with 184 additions and 189 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -4,15 +4,10 @@
|
|||
# Editor
|
||||
.vscode/
|
||||
.idea/
|
||||
cmake-build-debug/
|
||||
hello-algo.iml
|
||||
*.dSYM/
|
||||
|
||||
# mkdocs files
|
||||
site/
|
||||
.cache/
|
||||
codes/scripts
|
||||
docs/overrides/
|
||||
|
||||
# python files
|
||||
__pycache__
|
||||
|
|
|
@ -54,7 +54,7 @@ int peek(ArrayQueue *queue) {
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
void offer(ArrayQueue *queue, int num) {
|
||||
void push(ArrayQueue *queue, int num) {
|
||||
if (size(queue) == capacity(queue)) {
|
||||
printf("队列已满\r\n");
|
||||
return;
|
||||
|
@ -93,11 +93,11 @@ int main() {
|
|||
ArrayQueue *queue = newArrayQueue(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
offer(queue, 1);
|
||||
offer(queue, 3);
|
||||
offer(queue, 2);
|
||||
offer(queue, 5);
|
||||
offer(queue, 4);
|
||||
push(queue, 1);
|
||||
push(queue, 3);
|
||||
push(queue, 2);
|
||||
push(queue, 5);
|
||||
push(queue, 4);
|
||||
printf("队列 queue = ");
|
||||
printArrayQueue(queue);
|
||||
|
||||
|
@ -120,7 +120,7 @@ int main() {
|
|||
|
||||
/* 测试环形数组 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
offer(queue, i);
|
||||
push(queue, i);
|
||||
poll(queue);
|
||||
printf("第 %d 轮入队 + 出队后 queue = ", i);
|
||||
printArrayQueue(queue);
|
||||
|
|
|
@ -42,7 +42,7 @@ public:
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
void offer(int num) {
|
||||
void push(int num) {
|
||||
if (queSize == queCapacity) {
|
||||
cout << "队列已满" << endl;
|
||||
return;
|
||||
|
@ -90,11 +90,11 @@ int main() {
|
|||
ArrayQueue* queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
queue->offer(1);
|
||||
queue->offer(3);
|
||||
queue->offer(2);
|
||||
queue->offer(5);
|
||||
queue->offer(4);
|
||||
queue->push(1);
|
||||
queue->push(3);
|
||||
queue->push(2);
|
||||
queue->push(5);
|
||||
queue->push(4);
|
||||
cout << "队列 queue = ";
|
||||
PrintUtil::printVector(queue->toVector());
|
||||
|
||||
|
@ -117,7 +117,7 @@ int main() {
|
|||
|
||||
/* 测试环形数组 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue->offer(i);
|
||||
queue->push(i);
|
||||
queue->poll();
|
||||
cout << "第 " << i << " 轮入队 + 出队后 queue = ";
|
||||
PrintUtil::printVector(queue->toVector());
|
||||
|
|
|
@ -35,7 +35,7 @@ public:
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
void offer(int num) {
|
||||
void push(int num) {
|
||||
// 尾结点后添加 num
|
||||
ListNode* node = new ListNode(num);
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
|
@ -88,11 +88,11 @@ int main() {
|
|||
LinkedListQueue* queue = new LinkedListQueue();
|
||||
|
||||
/* 元素入队 */
|
||||
queue->offer(1);
|
||||
queue->offer(3);
|
||||
queue->offer(2);
|
||||
queue->offer(5);
|
||||
queue->offer(4);
|
||||
queue->push(1);
|
||||
queue->push(3);
|
||||
queue->push(2);
|
||||
queue->push(5);
|
||||
queue->push(4);
|
||||
cout << "队列 queue = ";
|
||||
PrintUtil::printVector(queue->toVector());
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ namespace hello_algo.chapter_stack_and_queue
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
public void offer(int num)
|
||||
public void push(int num)
|
||||
{
|
||||
if (queSize == capacity())
|
||||
{
|
||||
|
@ -97,11 +97,11 @@ namespace hello_algo.chapter_stack_and_queue
|
|||
ArrayQueue queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
Console.WriteLine("队列 queue = " + string.Join(",", queue.toArray()));
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
@ -123,7 +123,7 @@ namespace hello_algo.chapter_stack_and_queue
|
|||
/* 测试环形数组 */
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
queue.offer(i);
|
||||
queue.push(i);
|
||||
queue.poll();
|
||||
Console.WriteLine("第 " + i + " 轮入队 + 出队后 queue = " + string.Join(",", queue.toArray()));
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ namespace hello_algo.chapter_stack_and_queue
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
public void offer(int num)
|
||||
public void push(int num)
|
||||
{
|
||||
// 尾结点后添加 num
|
||||
ListNode node = new ListNode(num);
|
||||
|
@ -97,11 +97,11 @@ namespace hello_algo.chapter_stack_and_queue
|
|||
LinkedListQueue queue = new LinkedListQueue();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
Console.WriteLine("队列 queue = " + String.Join(",", queue.toArray()));
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
|
|
@ -32,8 +32,8 @@ func (q *arrayQueue) isEmpty() bool {
|
|||
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
|
||||
|
|
|
@ -54,11 +54,11 @@ func TestLinkedListDeque(t *testing.T) {
|
|||
deque := newLinkedListDeque()
|
||||
|
||||
// 元素入队
|
||||
deque.offerLast(2)
|
||||
deque.offerLast(5)
|
||||
deque.offerLast(4)
|
||||
deque.offerFirst(3)
|
||||
deque.offerFirst(1)
|
||||
deque.pushLast(2)
|
||||
deque.pushLast(5)
|
||||
deque.pushLast(4)
|
||||
deque.pushFirst(3)
|
||||
deque.pushFirst(1)
|
||||
fmt.Print("队列 deque = ")
|
||||
PrintList(deque.toList())
|
||||
|
||||
|
@ -87,12 +87,12 @@ func TestLinkedListDeque(t *testing.T) {
|
|||
|
||||
// BenchmarkArrayQueue 67.92 ns/op in Mac M1 Pro
|
||||
func BenchmarkLinkedListDeque(b *testing.B) {
|
||||
stack := newLinkedListDeque()
|
||||
deque := newLinkedListDeque()
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.offerLast(777)
|
||||
deque.pushLast(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.pollFirst()
|
||||
deque.pollFirst()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,13 +20,13 @@ func newLinkedListDeque() *linkedListDeque {
|
|||
}
|
||||
}
|
||||
|
||||
// offerFirst 队首元素入队
|
||||
func (s *linkedListDeque) offerFirst(value any) {
|
||||
// pushFirst 队首元素入队
|
||||
func (s *linkedListDeque) pushFirst(value any) {
|
||||
s.data.PushFront(value)
|
||||
}
|
||||
|
||||
// offerLast 队尾元素入队
|
||||
func (s *linkedListDeque) offerLast(value any) {
|
||||
// pushLast 队尾元素入队
|
||||
func (s *linkedListDeque) pushLast(value any) {
|
||||
s.data.PushBack(value)
|
||||
}
|
||||
|
||||
|
|
|
@ -21,8 +21,8 @@ func newLinkedListQueue() *linkedListQueue {
|
|||
}
|
||||
}
|
||||
|
||||
// offer 入队
|
||||
func (s *linkedListQueue) offer(value any) {
|
||||
// push 入队
|
||||
func (s *linkedListQueue) push(value any) {
|
||||
s.data.PushBack(value)
|
||||
}
|
||||
|
||||
|
|
|
@ -51,11 +51,11 @@ func TestArrayQueue(t *testing.T) {
|
|||
queue := newArrayQueue(capacity)
|
||||
|
||||
// 元素入队
|
||||
queue.offer(1)
|
||||
queue.offer(3)
|
||||
queue.offer(2)
|
||||
queue.offer(5)
|
||||
queue.offer(4)
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
fmt.Print("队列 queue = ")
|
||||
PrintSlice(queue.toSlice())
|
||||
|
||||
|
@ -78,7 +78,7 @@ func TestArrayQueue(t *testing.T) {
|
|||
|
||||
/* 测试环形数组 */
|
||||
for i := 0; i < 10; i++ {
|
||||
queue.offer(i)
|
||||
queue.push(i)
|
||||
queue.poll()
|
||||
fmt.Print("第", i, "轮入队 + 出队后 queue =")
|
||||
PrintSlice(queue.toSlice())
|
||||
|
@ -90,11 +90,11 @@ func TestLinkedListQueue(t *testing.T) {
|
|||
queue := newLinkedListQueue()
|
||||
|
||||
// 元素入队
|
||||
queue.offer(1)
|
||||
queue.offer(3)
|
||||
queue.offer(2)
|
||||
queue.offer(5)
|
||||
queue.offer(4)
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
fmt.Print("队列 queue = ")
|
||||
PrintList(queue.toList())
|
||||
|
||||
|
@ -119,24 +119,24 @@ func TestLinkedListQueue(t *testing.T) {
|
|||
// BenchmarkArrayQueue 8 ns/op in Mac M1 Pro
|
||||
func BenchmarkArrayQueue(b *testing.B) {
|
||||
capacity := 1000
|
||||
stack := newArrayQueue(capacity)
|
||||
queue := newArrayQueue(capacity)
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.offer(777)
|
||||
queue.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.poll()
|
||||
queue.poll()
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkLinkedQueue 62.66 ns/op in Mac M1 Pro
|
||||
func BenchmarkLinkedQueue(b *testing.B) {
|
||||
stack := newLinkedListQueue()
|
||||
queue := newLinkedListQueue()
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.offer(777)
|
||||
queue.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.poll()
|
||||
queue.poll()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ func TestStack(t *testing.T) {
|
|||
stack = append(stack, 2)
|
||||
stack = append(stack, 5)
|
||||
stack = append(stack, 4)
|
||||
fmt.Print("栈 = ")
|
||||
fmt.Print("栈 stack = ")
|
||||
PrintSlice(stack)
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
|
|
|
@ -35,7 +35,7 @@ class ArrayQueue {
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
public void offer(int num) {
|
||||
public void push(int num) {
|
||||
if (queSize == capacity()) {
|
||||
System.out.println("队列已满");
|
||||
return;
|
||||
|
@ -82,11 +82,11 @@ public class array_queue {
|
|||
ArrayQueue queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
System.out.println("队列 queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
@ -107,7 +107,7 @@ public class array_queue {
|
|||
|
||||
/* 测试环形数组 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.offer(i);
|
||||
queue.push(i);
|
||||
queue.poll();
|
||||
System.out.println("第 " + i + " 轮入队 + 出队后 queue = " + Arrays.toString(queue.toArray()));
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ class LinkedListDeque {
|
|||
}
|
||||
|
||||
/* 入队操作 */
|
||||
private void offer(int num, boolean isFront) {
|
||||
private void push(int num, boolean isFront) {
|
||||
ListNode node = new ListNode(num);
|
||||
// 若链表为空,则令 front, rear 都指向 node
|
||||
if (isEmpty())
|
||||
|
@ -61,13 +61,13 @@ class LinkedListDeque {
|
|||
}
|
||||
|
||||
/* 队首入队 */
|
||||
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);
|
||||
}
|
||||
|
||||
/* 出队操作 */
|
||||
|
@ -141,9 +141,9 @@ public class linkedlist_deque {
|
|||
public static void main(String[] args) {
|
||||
/* 初始化双向队列 */
|
||||
LinkedListDeque deque = new LinkedListDeque();
|
||||
deque.offerLast(3);
|
||||
deque.offerLast(2);
|
||||
deque.offerLast(5);
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
System.out.print("双点队列 deque = ");
|
||||
deque.print();
|
||||
|
||||
|
@ -154,10 +154,10 @@ public class linkedlist_deque {
|
|||
System.out.println("队尾元素 peekLast = " + peekLast);
|
||||
|
||||
/* 元素入队 */
|
||||
deque.offerLast(4);
|
||||
deque.pushLast(4);
|
||||
System.out.print("元素 4 队尾入队后 deque = ");
|
||||
deque.print();
|
||||
deque.offerFirst(1);
|
||||
deque.pushFirst(1);
|
||||
System.out.print("元素 1 队首入队后 deque = ");
|
||||
deque.print();
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
import include.*;
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
|
@ -30,7 +29,7 @@ class LinkedListQueue {
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
public void offer(int num) {
|
||||
public void push(int num) {
|
||||
// 尾结点后添加 num
|
||||
ListNode node = new ListNode(num);
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
|
@ -79,11 +78,11 @@ public class linkedlist_queue {
|
|||
LinkedListQueue queue = new LinkedListQueue();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
System.out.println("队列 queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
|
|
@ -30,7 +30,7 @@ class ArrayQueue {
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
offer(num) {
|
||||
push(num) {
|
||||
if (this.size == this.capacity)
|
||||
throw new Error("队列已满");
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
|
@ -75,11 +75,11 @@ const capacity = 10;
|
|||
const queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log("队列 queue =", queue.toArray());
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
@ -100,7 +100,7 @@ console.log("队列是否为空 = " + empty);
|
|||
|
||||
/* 测试环形数组 */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
queue.offer(i);
|
||||
queue.push(i);
|
||||
queue.poll();
|
||||
console.log("第 " + i + " 轮入队 + 出队后 queue =", queue.toArray());
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ class LinkedListQueue {
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
offer(num) {
|
||||
push(num) {
|
||||
// 尾结点后添加 num
|
||||
const node = new ListNode(num);
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
|
@ -78,11 +78,11 @@ class LinkedListQueue {
|
|||
const queue = new LinkedListQueue();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log("队列 queue = " + queue.toArray());
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
|
1
codes/python/.gitignore
vendored
Normal file
1
codes/python/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
__pycache__
|
|
@ -31,7 +31,7 @@ class ArrayQueue {
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
func offer(num: Int) {
|
||||
func push(num: Int) {
|
||||
if size() == capacity() {
|
||||
print("队列已满")
|
||||
return
|
||||
|
@ -82,11 +82,11 @@ enum _ArrayQueue {
|
|||
let queue = ArrayQueue(capacity: capacity)
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(num: 1)
|
||||
queue.offer(num: 3)
|
||||
queue.offer(num: 2)
|
||||
queue.offer(num: 5)
|
||||
queue.offer(num: 4)
|
||||
queue.push(num: 1)
|
||||
queue.push(num: 3)
|
||||
queue.push(num: 2)
|
||||
queue.push(num: 5)
|
||||
queue.push(num: 4)
|
||||
print("队列 queue = \(queue.toArray())")
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
@ -107,7 +107,7 @@ enum _ArrayQueue {
|
|||
|
||||
/* 测试环形数组 */
|
||||
for i in 0 ..< 10 {
|
||||
queue.offer(num: i)
|
||||
queue.push(num: i)
|
||||
queue.poll()
|
||||
print("第 \(i) 轮入队 + 出队后 queue = \(queue.toArray())")
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ class LinkedListQueue {
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
func offer(num: Int) {
|
||||
func push(num: Int) {
|
||||
// 尾结点后添加 num
|
||||
let node = ListNode(x: num)
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
|
@ -79,11 +79,11 @@ enum _LinkedListQueue {
|
|||
let queue = LinkedListQueue()
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(num: 1)
|
||||
queue.offer(num: 3)
|
||||
queue.offer(num: 2)
|
||||
queue.offer(num: 5)
|
||||
queue.offer(num: 4)
|
||||
queue.push(num: 1)
|
||||
queue.push(num: 3)
|
||||
queue.push(num: 2)
|
||||
queue.push(num: 5)
|
||||
queue.push(num: 4)
|
||||
print("队列 queue = \(queue.toArray())")
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
|
|
@ -31,7 +31,7 @@ class ArrayQueue {
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
offer(num: number): void {
|
||||
push(num: number): void {
|
||||
if (this.size == this.capacity)
|
||||
throw new Error("队列已满");
|
||||
// 计算尾指针,指向队尾索引 + 1
|
||||
|
@ -74,11 +74,11 @@ const capacity = 10;
|
|||
const queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log("队列 queue =", queue.toArray());
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
@ -99,7 +99,7 @@ console.log("队列是否为空 = " + empty);
|
|||
|
||||
/* 测试环形数组 */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
queue.offer(i);
|
||||
queue.push(i);
|
||||
queue.poll();
|
||||
console.log("第 " + i + " 轮入队 + 出队后 queue =", queue.toArray());
|
||||
console.log(queue.toArray());
|
||||
|
|
|
@ -28,7 +28,7 @@ class LinkedListQueue {
|
|||
}
|
||||
|
||||
/* 入队 */
|
||||
offer(num: number): void {
|
||||
push(num: number): void {
|
||||
// 尾结点后添加 num
|
||||
const node = new ListNode(num);
|
||||
// 如果队列为空,则令头、尾结点都指向该结点
|
||||
|
@ -78,11 +78,11 @@ class LinkedListQueue {
|
|||
const queue = new LinkedListQueue();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log("队列 queue = " + queue.toArray());
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
|
|
@ -50,7 +50,7 @@ pub fn ArrayQueue(comptime T: type) type {
|
|||
}
|
||||
|
||||
// 入队
|
||||
pub fn offer(self: *Self, num: T) !void {
|
||||
pub fn push(self: *Self, num: T) !void {
|
||||
if (self.size() == self.capacity()) {
|
||||
std.debug.print("队列已满\n", .{});
|
||||
return;
|
||||
|
@ -102,11 +102,11 @@ pub fn main() !void {
|
|||
defer queue.deinit();
|
||||
|
||||
// 元素入队
|
||||
try queue.offer(1);
|
||||
try queue.offer(3);
|
||||
try queue.offer(2);
|
||||
try queue.offer(5);
|
||||
try queue.offer(4);
|
||||
try queue.push(1);
|
||||
try queue.push(3);
|
||||
try queue.push(2);
|
||||
try queue.push(5);
|
||||
try queue.push(4);
|
||||
std.debug.print("队列 queue = ", .{});
|
||||
inc.PrintUtil.printArray(i32, try queue.toArray());
|
||||
|
||||
|
@ -130,7 +130,7 @@ pub fn main() !void {
|
|||
// 测试环形数组
|
||||
var i: i32 = 0;
|
||||
while (i < 10) : (i += 1) {
|
||||
try queue.offer(i);
|
||||
try queue.push(i);
|
||||
_ = queue.poll();
|
||||
std.debug.print("\n第 {} 轮入队 + 出队后 queue = ", .{i});
|
||||
inc.PrintUtil.printArray(i32, try queue.toArray());
|
||||
|
|
|
@ -62,7 +62,7 @@ pub fn LinkedListDeque(comptime T: type) type {
|
|||
}
|
||||
|
||||
// 入队操作
|
||||
pub fn offer(self: *Self, num: T, isFront: bool) !void {
|
||||
pub fn push(self: *Self, num: T, isFront: bool) !void {
|
||||
var node = try self.mem_allocator.create(ListNode(T));
|
||||
node.init(num);
|
||||
// 若链表为空,则令 front, rear 都指向 node
|
||||
|
@ -86,13 +86,13 @@ pub fn LinkedListDeque(comptime T: type) type {
|
|||
}
|
||||
|
||||
// 队首入队
|
||||
pub fn offerFirst(self: *Self, num: T) !void {
|
||||
try self.offer(num, true);
|
||||
pub fn pushFirst(self: *Self, num: T) !void {
|
||||
try self.push(num, true);
|
||||
}
|
||||
|
||||
// 队尾入队
|
||||
pub fn offerLast(self: *Self, num: T) !void {
|
||||
try self.offer(num, false);
|
||||
pub fn pushLast(self: *Self, num: T) !void {
|
||||
try self.push(num, false);
|
||||
}
|
||||
|
||||
// 出队操作
|
||||
|
@ -187,7 +187,7 @@ pub fn main() !void {
|
|||
|
||||
// 队尾入队
|
||||
for (nums) |num| {
|
||||
try deque.offerLast(num);
|
||||
try deque.pushLast(num);
|
||||
std.debug.print("\n元素 {} 队尾入队后,队列为\n", .{num});
|
||||
try deque.print();
|
||||
}
|
||||
|
@ -203,7 +203,7 @@ pub fn main() !void {
|
|||
|
||||
// 队首入队
|
||||
for (nums) |num| {
|
||||
try deque.offerFirst(num);
|
||||
try deque.pushFirst(num);
|
||||
std.debug.print("\n元素 {} 队首入队后,队列为\n", .{num});
|
||||
try deque.print();
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ pub fn LinkedListQueue(comptime T: type) type {
|
|||
}
|
||||
|
||||
// 入队
|
||||
pub fn offer(self: *Self, num: T) !void {
|
||||
pub fn push(self: *Self, num: T) !void {
|
||||
// 尾结点后添加 num
|
||||
var node = try self.mem_allocator.create(inc.ListNode(T));
|
||||
node.init(num);
|
||||
|
@ -98,11 +98,11 @@ pub fn main() !void {
|
|||
defer queue.deinit();
|
||||
|
||||
// 元素入队
|
||||
try queue.offer(1);
|
||||
try queue.offer(3);
|
||||
try queue.offer(2);
|
||||
try queue.offer(5);
|
||||
try queue.offer(4);
|
||||
try queue.push(1);
|
||||
try queue.push(3);
|
||||
try queue.push(2);
|
||||
try queue.push(5);
|
||||
try queue.push(4);
|
||||
std.debug.print("队列 queue = ", .{});
|
||||
inc.PrintUtil.printArray(i32, try queue.toArray());
|
||||
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 61 KiB After Width: | Height: | Size: 65 KiB |
|
@ -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 |
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue