Documentation
¶
Overview ¶
Package queue provides several implementations of a queue data structure.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PriorityQueue ¶
type PriorityQueue interface {
// Enqueue adds an element to the priority queue with the given priority.
Enqueue(element any, priority int) (ok bool)
// Dequeue removes and returns the element with the highest priority from the priority queue.
Dequeue() (element any, ok bool)
// Peek returns the element with the highest priority from the priority queue without removing it.
Peek() (element any, ok bool)
// Empty checks if the priority queue is empty.
Empty() bool
// Full checks if the priority queue is full.
Full() bool
// Size returns the number of elements in the priority queue.
Size() int
}
PriorityQueue is an interface for a priority queue.
Example ¶
pq, _ := NewPriorityQueue(3) pq.Enqueue(100, 3) pq.Enqueue(80, 22) pq.Enqueue(75, 1) v, _ := pq.Dequeue() fmt.Println(v) v, _ = pq.Dequeue() fmt.Println(v) v, _ = pq.Dequeue() fmt.Println(v)
Output: 80 100 75
func NewPriorityQueue ¶
func NewPriorityQueue(capacity int) (q PriorityQueue, err error)
NewPriorityQueue creates a new priority queue with the given capacity.
type Queue ¶
type Queue interface {
// Enqueue adds an element to the end of the queue.
Enqueue(element any) (ok bool)
// Dequeue removes and returns the element at the front of the queue.
Dequeue() (element any, ok bool)
// Peek returns the element at the front of the queue without removing it.
Peek() (element any, ok bool)
// Empty checks if the queue is empty.
Empty() bool
// Full checks if the queue is full.
Full() bool
}
Queue is an interface for a queue data structure.
Example ¶
s := NewQueue(3)
for i := 0; i < 3; i++ {
s.Enqueue(i)
}
for i := 0; i < 3; i++ {
fmt.Println(s.Dequeue())
}
Output: 0 true 1 true 2 true
func NewLoopQueue ¶
NewLoopQueue creates a new loop queue with the specified capacity.
Example ¶
s, _ := NewLoopQueue(3)
for i := 0; i < 3; i++ {
s.Enqueue(i)
}
for i := 0; i < 3; i++ {
fmt.Println(s.Dequeue())
}
Output: 0 true 1 true 2 true
Click to show internal directories.
Click to hide internal directories.