queue

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

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[T any] interface {
	// Enqueue adds an element with a certain priority to the queue.
	Enqueue(element T, priority int) (ok bool)
	// Dequeue removes and returns the element with the highest priority from the queue.
	Dequeue() (element T, ok bool)
	// Peek returns the element with the highest priority from the queue without removing it.
	Peek() (element T, ok bool)
	// Empty checks if the queue is empty.
	Empty() bool
	// Full checks if the queue is full.
	Full() bool
	// Size returns the current size of the queue.
	Size() int
}

PriorityQueue defines the methods of a priority queue data structure.

Example
pq, _ := NewPriorityQueue[int](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[T any](capacity int) (q PriorityQueue[T], err error)

NewPriorityQueue creates a new priority queue with the specified capacity.

type Queue

type Queue[T any] interface {
	// Enqueue adds an element to the end of the queue
	Enqueue(element T) (ok bool)
	// Dequeue removes and returns the element at the front of the queue
	Dequeue() (element T, ok bool)
	// Peek returns the element at the front of the queue without removing it
	Peek() (element T, ok bool)
	// Empty checks if the queue is empty
	Empty() bool
	// Full checks if the queue is full
	Full() bool
}

Queue is the interface for a generic queue data structure

Example
s := NewQueue[int](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

func NewLoopQueue[T any](capacity int) (q Queue[T], err error)

NewLoopQueue creates a new loopQueue with the specified capacity.

Example
s, _ := NewLoopQueue[int](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 NewQueue

func NewQueue[T any](capacity int) Queue[T]

NewQueue creates a new linear queue with the specified capacity

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL