Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions go/09_queue/QueueBasedOnLinkedList.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ func (this *LinkedListQueue) DeQueue() interface{} {
if this.head == nil {
return nil
}

v := this.head.val
this.head = this.head.next

// fix: keep the head and tail the same
if this.head == nil {
this.tail = nil
}

this.length--
return v
}
Expand Down
23 changes: 23 additions & 0 deletions go/09_queue/QueueBasedOnLinkedList_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,37 @@ func TestListQueue_DeQueue(t *testing.T) {
q.EnQueue(5)
q.EnQueue(6)
t.Log(q)

// 1
q.DeQueue()
t.Log(q)

// 2
q.DeQueue()
t.Log(q)

// 3
q.DeQueue()
t.Log(q)

// 4
q.DeQueue()
t.Log(q)

// 5
q.DeQueue()
t.Log(q)

// 6 note: all of the nodes are dequeue
// head = tail = nil
q.DeQueue()
t.Log(q)

// enqueue
q.EnQueue(1)
t.Log(q)

// head = tail = nil
q.DeQueue()
t.Log(q)
}