mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 03:30:30 +08:00
Unify the function naming of
queue from `offer()` to `push()`
This commit is contained in:
@@ -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)
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
|
||||
Reference in New Issue
Block a user