queue

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: MIT Imports: 2 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

type Queue[T any] interface {
	// Len returns length of Queue
	Len() int
	// Push pushes element to end of Queue
	Push(v T)
	// Pop pops element from head of Queue
	Pop() (T, bool)
	// Head returns head element of Queue
	Head() (T, bool)
	// Tail returns tail element of Queue
	Tail() (T, bool)
	// Range calls f sequentially for each element present in the Queue.
	// If f returns false, range stops the iteration.
	Range(f func(T) bool)
	// Clear releases all the entries, resulting in an empty Queue.
	Clear()
}
Example
package main

import (
	"fmt"

	"github.com/xoctopus/x/container/queue"
)

func main() {
	for _, q := range []queue.Queue[any]{
		queue.NewSafeQueue[any](),
		queue.NewQueue[any](),
	} {
		q.Push(1)
		q.Push(2)
		q.Push(3)
		q.Push(4)

		head, _ := q.Head()
		tail, _ := q.Tail()
		fmt.Println("head:", head)
		fmt.Println("tail:", tail)

		v, _ := q.Pop()
		fmt.Println(v)

		for v = range q.Range {
			fmt.Println(v)
			continue
		}

		q.Clear()
		q.Len()
		_, ok := q.Head()
		fmt.Println("head:", ok)
		_, ok = q.Tail()
		fmt.Println("tail:", ok)
		_, ok = q.Pop()
		fmt.Println("pop: ", ok)
	}

}
Output:
head: 1
tail: 4
1
2
3
4
head: false
tail: false
pop:  false
head: 1
tail: 4
1
2
3
4
head: false
tail: false
pop:  false

func NewQueue

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

func NewSafeQueue

func NewSafeQueue[T any]() Queue[T]

Jump to

Keyboard shortcuts

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