gqueue

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2024 License: MIT Imports: 3 Imported by: 0

README

gqueue

Generic Queue implementation in Go using linked list

Usage

s := gqueue.New(1, 2, 3, 4)
fmt.Println(s.Len())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
fmt.Println(s.Pop())
s.Push(5)
fmt.Println(s.Peek())
// Output: 4
// 1
// 2
// 3
// 4
// 5

Changelog

v0.1.0
  • minimal supported version of Go is 1.20
  • initial implementation
v1.0.0
  • minimal supported version of Go is 1.23
  • added Iter method to iterate over the queue and consume all items
s := gqueue.New(1, 2, 3, 4)
for v := range s.Iter() {
    fmt.Println(v)
}
fmt.Println("len:", s.Len())
// Output: 1
// 2
// 3
// 4
// len: 0
v1.1.0
  • added IsEmpty method to check if the queue is empty

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GQueue

type GQueue[T any] struct {
	// contains filtered or unexported fields
}

GQueue is generic Queue implementation using linked list

Example
package main

import (
	"fmt"

	"github.com/sv-tools/gqueue"
)

func main() {
	s := gqueue.New(1, 2, 3, 4)
	fmt.Println(s.Len())
	fmt.Println(s.Pop())
	fmt.Println(s.Pop())
	fmt.Println(s.Pop())
	fmt.Println(s.Pop())
	s.Push(5)
	fmt.Println(s.Peek())
}
Output:

4
1
2
3
4
5

func New

func New[T any](items ...T) GQueue[T]

New creates an instance of GQueue and pushes all given items to the created queue

func (*GQueue[T]) Clear

func (s *GQueue[T]) Clear()

Clear clears the queue

func (*GQueue[T]) IsEmpty added in v1.1.0

func (s *GQueue[T]) IsEmpty() bool

IsEmpty returns true if the queue is empty

func (*GQueue[T]) Iter added in v1.0.0

func (s *GQueue[T]) Iter() iter.Seq[T]

Iter returns a consuming iterator for the queue

Example
package main

import (
	"fmt"

	"github.com/sv-tools/gqueue"
)

func main() {
	s := gqueue.New(1, 2, 3, 4)
	for v := range s.Iter() {
		fmt.Println(v)
	}
	fmt.Println("len:", s.Len())
}
Output:

1
2
3
4
len: 0

func (*GQueue[T]) Len

func (s *GQueue[T]) Len() int

Len returns the length of the stack

func (*GQueue[T]) Peek

func (s *GQueue[T]) Peek() T

Peek returns the first item without removing it or a zero object of given type

func (*GQueue[T]) Pop

func (s *GQueue[T]) Pop() T

Pop returns the first item and removes it from the queue or returns a zero object of given type

func (*GQueue[T]) Push

func (s *GQueue[T]) Push(item T)

Push puts the given item into the queue

func (*GQueue[T]) String

func (s *GQueue[T]) String() string

String returns the list of all items in text format

Jump to

Keyboard shortcuts

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