Skip to content

Commit

Permalink
fix(Go): Update array_queue.go and array_deque.go (#1362)
Browse files Browse the repository at this point in the history
* 🐞 fix: 队列为空时不应该操作指向

* 🧪 test: 添加pop测试用例

* 🔧 build: 修改testify依赖包

* 🐞 fix: 双向队列为空时,pop不操作指向

* 🔧 build:

Remove third-party packages

* Delete codes/go/go.sum

---------

Co-authored-by: Yudong Jin <krahets@163.com>
  • Loading branch information
XiaoK29 and krahets committed May 31, 2024
1 parent 3f4220d commit 0774920
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions codes/go/chapter_stack_and_queue/array_deque.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func (q *arrayDeque) pushLast(num int) {
/* 队首出队 */
func (q *arrayDeque) popFirst() any {
num := q.peekFirst()
if num == nil {
return nil
}
// 队首指针向后移动一位
q.front = q.index(q.front + 1)
q.queSize--
Expand All @@ -81,6 +84,9 @@ func (q *arrayDeque) popFirst() any {
/* 队尾出队 */
func (q *arrayDeque) popLast() any {
num := q.peekLast()
if num == nil {
return nil
}
q.queSize--
return num
}
Expand Down
4 changes: 4 additions & 0 deletions codes/go/chapter_stack_and_queue/array_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (q *arrayQueue) push(num int) {
/* 出队 */
func (q *arrayQueue) pop() any {
num := q.peek()
if num == nil {
return nil
}

// 队首指针向后移动一位,若越过尾部,则返回到数组头部
q.front = (q.front + 1) % q.queCapacity
q.queSize--
Expand Down
4 changes: 4 additions & 0 deletions codes/go/chapter_stack_and_queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ func TestQueue(t *testing.T) {
}

func TestArrayQueue(t *testing.T) {

// 初始化队列,使用队列的通用接口
capacity := 10
queue := newArrayQueue(capacity)
if queue.pop() != nil {
t.Errorf("want:%v,got:%v", nil, queue.pop())
}

// 元素入队
queue.push(1)
Expand Down

0 comments on commit 0774920

Please sign in to comment.