stacks

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: BSD-3-Clause Imports: 0 Imported by: 2

README

stacks

Go Reference License Build

stacks is a Go library with generics implementing multiple Stack data structures for optimizing by use-case.

stack (noun):
  1. a LIFO data structure
Getting Started

The stacks package can be added to a Go project with go get.

go get cattlecloud.net/go/stacks@latest
import "cattlecloud.net/go/stacks"
Examples
backed by slice

The Simple stack type is backed by a normal Go slice and is a good choice for small numbers of elements or where the size of the stack will not increase or decrease repeatedly - this is important so as to minmize re-copying the data as the underlying slice is resized.

s := stacks.Simple[string]()
s.Push("how")
s.Push("are")
s.Push("you")
fmt.Println(s.Pop()) // "you"
backed by linked list

The Linked stack type is backed by a linked-list and is a good choice for large numbers of elements or where the size of the stack does increase or decrease repeatedly. The nature of the linked list avoids the need to re-copy elements as the size of the data structure changes over time.

s := stacks.Linked[string]()
s.Push("foo")
s.Push("bar")
s.Push("baz")
fmt.Println(s.Pop()) // "baz"
methods
s.Push[T](item T)
s.Pop() T
s.Peek() T
s.Empty() bool
s.Size() int
License

The cattlecloud.net/go/stacks module is opensource under the BSD-3-Clause license.

Documentation

Overview

Package stacks provides various implementations of a stack data structure, each ideal for a certain set of use cases and conditions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stack

type Stack[T any] interface {
	Push(T)
	Pop() T
	Peek() T
	Empty() bool
	Size() int
}

A Stack implements the basic stack data structure operations, including Push, Pop, Peek, Empty, and Size.

func Linked

func Linked[T any](items ...T) Stack[T]

Linked creates an implementation of Stack backed by a linked-list. This is a good choice for use cases where the size of the stack changes dramatically and often. Unlike a slice, the backing data structure will not need to be copied as the size of the stack changes, as each element is allocated individually in the linked list.

func Simple

func Simple[T any](items ...T) Stack[T]

Simple creates an implementation of Stack backed by a slice. This is a good choice for small numbers of elements or where the size of the stack will not increase or decrease substantially.

Jump to

Keyboard shortcuts

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