queue
queue is a lightweight package that provides queue and deque implementation with high performance in Go.
It also performs better than the built-in container/list package for any operation.
Documentation
Install
go get github.com/erfanmomeniii/queue
Next, include it in your application:
import "github.com/erfanmomeniii/queue"
Quick Start
The following example illustrates how to use this package for creating an instance and performing any operation with it:
package main
import (
"fmt"
"github.com/erfanmomeniii/queue"
)
func main() {
q := queue.New()
q.PushFront(1)
q.PushFront("hi")
q.PushBack(3.14)
// q => [ "hi", 1, 3.14]
fmt.Println(q.Front())
// hi
fmt.Println(q.Back())
// 3.14
q.PopBack()
q.PopFront()
fmt.Println(q.Front())
// 1
}
Usage
type Queue
type Queue struct {
front *Node
back *Node
}
Queue is an instantiation of the queue.
func New
func New() *Queue
New creates a new instance of a queue.
func PushFront
func (q *Queue) PushFront(value any)
PushFront adds a new element at the beginning of the queue.
func PushBack
func (q *Queue) PushBack(value any)
PushBack adds a new element at the end of the queue.
func PopFront
func (q *Queue) PopFront() (value any)
PopFront retrieves and removes the value of the first element in the queue.
func PopBack
func (q *Queue) PopBack() (value any)
PopBack retrieves and removes the value of the last element in the queue.
func Front
func (q *Queue) Front() (value any)
Front returns the value of front elements of the queue.
func Back
func (q *Queue) Back() (value any)
Back returns the value of last elements of the queue.
func Size
func (q *Queue) Size() (s int)
Size returns size of the queue.
Benchmarks
$ cd benchmarks
$ go test -bench .
Contributing
Pull requests are welcome. For changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.