Documentation
¶
Overview ¶
Package circularqueue implements a thread-safe circular queue
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Cicular queue
func (*Queue) Cap ¶
Cap returns the capacity (the maximum number of elements) of the queue
Example ¶
q := NewQueue(10)
q.Push("abc")
q.Push("def")
q.Push("ghi")
fmt.Println(q.Cap())
Output: 10
func (*Queue) Len ¶
Returns the length (the number of elements) of the queue
Example ¶
q := NewQueue(10)
q.Push("abc")
q.Push("def")
q.Push("ghi")
fmt.Println(q.Len())
Output: 3
func (*Queue) Pop ¶
Pops takes and removes the last element from the queue
Example ¶
q := NewQueue(10)
q.Push("abc")
q.Push("def")
val1, err1 := q.Pop()
val2, err2 := q.Pop()
val3, err3 := q.Pop()
fmt.Println(val1, err1)
fmt.Println(val2, err2)
fmt.Println(val3, err3)
Output: def <nil> abc <nil> <nil> Can't pop: queue is empty
func (*Queue) PopAt ¶
PopAt takes and removes the element at the index position from the queue
index = 0 to take the first element (the head) index = N to take the Nth element from the head index = -1 to take the last element (the tail) index = -N to take the Nth element from the tail
Example ¶
q := NewQueue(10)
q.Push("abc")
q.Push("def")
val1, err1 := q.PopAt(0)
val2, err2 := q.PopAt(0)
val3, err3 := q.PopAt(0)
fmt.Println(val1, err1)
fmt.Println(val2, err2)
fmt.Println(val3, err3)
Output: abc <nil> def <nil> <nil> Can't pop: queue is empty
func (*Queue) Push ¶
Push appends the element to the end of the queue
Example ¶
q := NewQueue(2)
vacant1, err1 := q.Push("abc")
vacant2, err2 := q.Push("def")
vacant3, err3 := q.Push("ghi")
fmt.Println(vacant1, err1)
fmt.Println(vacant2, err2)
fmt.Println(vacant3, err3)
Output: 1 <nil> 0 <nil> 0 Can't push: queue is full (capacity=2)
Click to show internal directories.
Click to hide internal directories.