diff --git a/.gitignore b/.gitignore index 52026b066..8b0e01afd 100644 --- a/.gitignore +++ b/.gitignore @@ -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__ diff --git a/codes/c/chapter_stack_and_queue/array_queue.c b/codes/c/chapter_stack_and_queue/array_queue.c index 74d45c849..f0edbe6af 100644 --- a/codes/c/chapter_stack_and_queue/array_queue.c +++ b/codes/c/chapter_stack_and_queue/array_queue.c @@ -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); diff --git a/codes/cpp/chapter_stack_and_queue/array_queue.cpp b/codes/cpp/chapter_stack_and_queue/array_queue.cpp index 1090905e1..8e92c620f 100644 --- a/codes/cpp/chapter_stack_and_queue/array_queue.cpp +++ b/codes/cpp/chapter_stack_and_queue/array_queue.cpp @@ -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()); diff --git a/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp b/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp index 6c830ff8e..3b1afc26c 100644 --- a/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp +++ b/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp @@ -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()); diff --git a/codes/csharp/chapter_stack_and_queue/array_queue.cs b/codes/csharp/chapter_stack_and_queue/array_queue.cs index 9491b0ef9..ca84dc93a 100644 --- a/codes/csharp/chapter_stack_and_queue/array_queue.cs +++ b/codes/csharp/chapter_stack_and_queue/array_queue.cs @@ -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())); } diff --git a/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs b/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs index 70816c8f0..4ee650fdc 100644 --- a/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs +++ b/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs @@ -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())); /* 访问队首元素 */ diff --git a/codes/go/chapter_stack_and_queue/array_queue.go b/codes/go/chapter_stack_and_queue/array_queue.go index 95970bc77..5213cb474 100644 --- a/codes/go/chapter_stack_and_queue/array_queue.go +++ b/codes/go/chapter_stack_and_queue/array_queue.go @@ -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 diff --git a/codes/go/chapter_stack_and_queue/deque_test.go b/codes/go/chapter_stack_and_queue/deque_test.go index 4c0f2f533..25deec25c 100644 --- a/codes/go/chapter_stack_and_queue/deque_test.go +++ b/codes/go/chapter_stack_and_queue/deque_test.go @@ -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() } } diff --git a/codes/go/chapter_stack_and_queue/linkedlist_deque.go b/codes/go/chapter_stack_and_queue/linkedlist_deque.go index b1db77ca6..763ebd943 100644 --- a/codes/go/chapter_stack_and_queue/linkedlist_deque.go +++ b/codes/go/chapter_stack_and_queue/linkedlist_deque.go @@ -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) } diff --git a/codes/go/chapter_stack_and_queue/linkedlist_queue.go b/codes/go/chapter_stack_and_queue/linkedlist_queue.go index f5d164a82..303674cd7 100644 --- a/codes/go/chapter_stack_and_queue/linkedlist_queue.go +++ b/codes/go/chapter_stack_and_queue/linkedlist_queue.go @@ -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) } diff --git a/codes/go/chapter_stack_and_queue/queue_test.go b/codes/go/chapter_stack_and_queue/queue_test.go index 0104cea03..c9dfa2d75 100644 --- a/codes/go/chapter_stack_and_queue/queue_test.go +++ b/codes/go/chapter_stack_and_queue/queue_test.go @@ -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() } } diff --git a/codes/go/chapter_stack_and_queue/stack_test.go b/codes/go/chapter_stack_and_queue/stack_test.go index a4cf338b7..d881c5ec8 100644 --- a/codes/go/chapter_stack_and_queue/stack_test.go +++ b/codes/go/chapter_stack_and_queue/stack_test.go @@ -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) /* 访问栈顶元素 */ diff --git a/codes/java/chapter_stack_and_queue/array_queue.java b/codes/java/chapter_stack_and_queue/array_queue.java index 030fb06bf..8552fd113 100644 --- a/codes/java/chapter_stack_and_queue/array_queue.java +++ b/codes/java/chapter_stack_and_queue/array_queue.java @@ -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())); } diff --git a/codes/java/chapter_stack_and_queue/linkedlist_deque.java b/codes/java/chapter_stack_and_queue/linkedlist_deque.java index 3c1503e46..c8e344c38 100644 --- a/codes/java/chapter_stack_and_queue/linkedlist_deque.java +++ b/codes/java/chapter_stack_and_queue/linkedlist_deque.java @@ -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(); diff --git a/codes/java/chapter_stack_and_queue/linkedlist_queue.java b/codes/java/chapter_stack_and_queue/linkedlist_queue.java index 82e2571c2..e7ac6a49c 100644 --- a/codes/java/chapter_stack_and_queue/linkedlist_queue.java +++ b/codes/java/chapter_stack_and_queue/linkedlist_queue.java @@ -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())); /* 访问队首元素 */ diff --git a/codes/javascript/chapter_stack_and_queue/array_queue.js b/codes/javascript/chapter_stack_and_queue/array_queue.js index 6dd278c66..fc74a958e 100644 --- a/codes/javascript/chapter_stack_and_queue/array_queue.js +++ b/codes/javascript/chapter_stack_and_queue/array_queue.js @@ -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()); } diff --git a/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js b/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js index 62c2a3834..5f17e99cc 100644 --- a/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js +++ b/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js @@ -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()); /* 访问队首元素 */ diff --git a/codes/python/.gitignore b/codes/python/.gitignore new file mode 100644 index 000000000..bee8a64b7 --- /dev/null +++ b/codes/python/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/codes/swift/chapter_stack_and_queue/array_queue.swift b/codes/swift/chapter_stack_and_queue/array_queue.swift index da5a75087..c04da4362 100644 --- a/codes/swift/chapter_stack_and_queue/array_queue.swift +++ b/codes/swift/chapter_stack_and_queue/array_queue.swift @@ -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())") } diff --git a/codes/swift/chapter_stack_and_queue/linkedlist_queue.swift b/codes/swift/chapter_stack_and_queue/linkedlist_queue.swift index 1d671742b..bc2d5231b 100644 --- a/codes/swift/chapter_stack_and_queue/linkedlist_queue.swift +++ b/codes/swift/chapter_stack_and_queue/linkedlist_queue.swift @@ -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())") /* 访问队首元素 */ diff --git a/codes/typescript/chapter_stack_and_queue/array_queue.ts b/codes/typescript/chapter_stack_and_queue/array_queue.ts index ff4892256..cd4bf9096 100644 --- a/codes/typescript/chapter_stack_and_queue/array_queue.ts +++ b/codes/typescript/chapter_stack_and_queue/array_queue.ts @@ -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()); diff --git a/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts b/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts index 6dabdbc0c..3c8759291 100644 --- a/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts +++ b/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts @@ -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()); /* 访问队首元素 */ diff --git a/codes/zig/chapter_stack_and_queue/array_queue.zig b/codes/zig/chapter_stack_and_queue/array_queue.zig index e9949a01e..8a3938f96 100644 --- a/codes/zig/chapter_stack_and_queue/array_queue.zig +++ b/codes/zig/chapter_stack_and_queue/array_queue.zig @@ -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()); diff --git a/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig b/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig index 3ac4ac722..3fe6c5b03 100644 --- a/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig +++ b/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig @@ -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(); } diff --git a/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig b/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig index 162958e3f..ea8735e6a 100644 --- a/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig +++ b/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig @@ -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()); diff --git a/docs/chapter_stack_and_queue/deque.assets/deque_operations.png b/docs/chapter_stack_and_queue/deque.assets/deque_operations.png index 242ea98c5..45ee775b4 100644 Binary files a/docs/chapter_stack_and_queue/deque.assets/deque_operations.png and b/docs/chapter_stack_and_queue/deque.assets/deque_operations.png differ diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index 0a1bdce07..6cdbbd95e 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -12,16 +12,16 @@ comments: true ## 5.3.1. 双向队列常用操作 -双向队列的常用操作见下表(方法命名以 Java 为例)。 +双向队列的常用操作见下表,方法名需根据特定语言来确定。

Table. 双向队列的常用操作

-| 方法 | 描述 | 时间复杂度 | +| 方法名 | 描述 | 时间复杂度 | | ------------ | ---------------- | ---------- | -| 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); } /* 出队操作 */ diff --git a/docs/chapter_stack_and_queue/queue.assets/queue_operations.png b/docs/chapter_stack_and_queue/queue.assets/queue_operations.png index b165cd10d..ad6347be8 100644 Binary files a/docs/chapter_stack_and_queue/queue.assets/queue_operations.png and b/docs/chapter_stack_and_queue/queue.assets/queue_operations.png differ diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index 9be40d978..d0ed36660 100644 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -14,19 +14,19 @@ comments: true ## 5.2.1. 队列常用操作 -队列的常用操作见下表(方法命名以 Java 为例)。 +队列的常用操作见下表,方法名需根据特定语言来确定。

Table. 队列的常用操作

-| 方法 | 描述 | 时间复杂度 | -| --------- | ---------------------------- | ---------- | -| 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)$ |
@@ -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