mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-05 11:41:22 +08:00
refactor: Replace poll with pop in Queue and Deque (#415)
This commit is contained in:
@@ -47,7 +47,7 @@ func (q *arrayQueue) push(num int) {
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
func (q *arrayQueue) poll() any {
|
||||
func (q *arrayQueue) pop() any {
|
||||
num := q.peek()
|
||||
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
||||
q.front = (q.front + 1) % q.queCapacity
|
||||
|
||||
@@ -112,11 +112,11 @@ func TestLinkedListDeque(t *testing.T) {
|
||||
fmt.Println("队尾元素 rear =", rear)
|
||||
|
||||
// 元素出队
|
||||
pollFirst := deque.pollFirst()
|
||||
fmt.Print("队首出队元素 pollFirst = ", pollFirst, ",队首出队后 deque = ")
|
||||
popFirst := deque.popFirst()
|
||||
fmt.Print("队首出队元素 popFirst = ", popFirst, ",队首出队后 deque = ")
|
||||
PrintList(deque.toList())
|
||||
pollLast := deque.pollLast()
|
||||
fmt.Print("队尾出队元素 pollLast = ", pollLast, ",队尾出队后 deque = ")
|
||||
popLast := deque.popLast()
|
||||
fmt.Print("队尾出队元素 popLast = ", popLast, ",队尾出队后 deque = ")
|
||||
PrintList(deque.toList())
|
||||
|
||||
// 获取队的长度
|
||||
@@ -136,6 +136,6 @@ func BenchmarkLinkedListDeque(b *testing.B) {
|
||||
deque.pushLast(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
deque.pollFirst()
|
||||
deque.popFirst()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func (s *linkedListDeque) pushLast(value any) {
|
||||
}
|
||||
|
||||
/* 队首元素出队 */
|
||||
func (s *linkedListDeque) pollFirst() any {
|
||||
func (s *linkedListDeque) popFirst() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
@@ -42,7 +42,7 @@ func (s *linkedListDeque) pollFirst() any {
|
||||
}
|
||||
|
||||
/* 队尾元素出队 */
|
||||
func (s *linkedListDeque) pollLast() any {
|
||||
func (s *linkedListDeque) popLast() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func (s *linkedListQueue) push(value any) {
|
||||
}
|
||||
|
||||
/* 出队 */
|
||||
func (s *linkedListQueue) poll() any {
|
||||
func (s *linkedListQueue) pop() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -31,9 +31,9 @@ func TestQueue(t *testing.T) {
|
||||
fmt.Println("队首元素 peek =", peek.Value)
|
||||
|
||||
/* 元素出队 */
|
||||
poll := queue.Front()
|
||||
queue.Remove(poll)
|
||||
fmt.Print("出队元素 poll = ", poll.Value, ",出队后 queue = ")
|
||||
pop := queue.Front()
|
||||
queue.Remove(pop)
|
||||
fmt.Print("出队元素 pop = ", pop.Value, ",出队后 queue = ")
|
||||
PrintList(queue)
|
||||
|
||||
/* 获取队列的长度 */
|
||||
@@ -64,8 +64,8 @@ func TestArrayQueue(t *testing.T) {
|
||||
fmt.Println("队首元素 peek =", peek)
|
||||
|
||||
// 元素出队
|
||||
poll := queue.poll()
|
||||
fmt.Print("出队元素 poll = ", poll, ", 出队后 queue = ")
|
||||
pop := queue.pop()
|
||||
fmt.Print("出队元素 pop = ", pop, ", 出队后 queue = ")
|
||||
PrintSlice(queue.toSlice())
|
||||
|
||||
// 获取队的长度
|
||||
@@ -79,7 +79,7 @@ func TestArrayQueue(t *testing.T) {
|
||||
/* 测试环形数组 */
|
||||
for i := 0; i < 10; i++ {
|
||||
queue.push(i)
|
||||
queue.poll()
|
||||
queue.pop()
|
||||
fmt.Print("第", i, "轮入队 + 出队后 queue =")
|
||||
PrintSlice(queue.toSlice())
|
||||
}
|
||||
@@ -103,8 +103,8 @@ func TestLinkedListQueue(t *testing.T) {
|
||||
fmt.Println("队首元素 peek =", peek)
|
||||
|
||||
// 元素出队
|
||||
poll := queue.poll()
|
||||
fmt.Print("出队元素 poll = ", poll, ", 出队后 queue = ")
|
||||
pop := queue.pop()
|
||||
fmt.Print("出队元素 pop = ", pop, ", 出队后 queue = ")
|
||||
PrintList(queue.toList())
|
||||
|
||||
// 获取队的长度
|
||||
@@ -125,7 +125,7 @@ func BenchmarkArrayQueue(b *testing.B) {
|
||||
queue.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.poll()
|
||||
queue.pop()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,6 +137,6 @@ func BenchmarkLinkedQueue(b *testing.B) {
|
||||
queue.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.poll()
|
||||
queue.pop()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user