Skip to content

Commit

Permalink
Refactor Queue to be based on simple slice.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtomasevic committed Jul 24, 2022
1 parent 459f334 commit eff4dc7
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions collections/queue.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package collections

type Queue[T any] struct {
elements []T
}
type Queue[T any] []T

// Enqueue Adds an object to the end of the Queue.
func (queue *Queue[T]) Enqueue(element T) {
queue.elements = append(queue.elements, element)
*queue = append(*queue, element)
}

// Dequeue Removes and returns the object at the beginning of the Queue.
Expand All @@ -15,8 +13,8 @@ func (queue *Queue[T]) Dequeue() *T {
if queue.IsEmpty() {
return nil
}
removed := queue.elements[0]
queue.elements = queue.elements[1:len(queue.elements)]
removed := (*queue)[0]
*queue = (*queue)[1:len(*queue)]
return &removed
}

Expand All @@ -26,9 +24,10 @@ func (queue *Queue[T]) Peek() *T {
if queue.IsEmpty() {
return nil
}
return &queue.elements[0]
return &(*queue)[0]
}

// IsEmpty return bool is true, otherwise false
func (queue *Queue[T]) IsEmpty() bool {
return len(queue.elements) == 0
return len(*queue) == 0
}

0 comments on commit eff4dc7

Please sign in to comment.