linkedstack

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option[E any] func(*Stack[E]) error

Option is funtional option type for configuring a Stack.

func WithSafe

func WithSafe[E any]() Option[E]

WithSafe creates a option that make the linkedlist-backed stack safe for concurrent use.

type Stack

type Stack[E any] struct {
	// contains filtered or unexported fields
}

Stack represents a stack based on linkedlist.. The stack provides typical LIFO(last-in, first-out) behavior.

func New

func New[E any](ops ...Option[E]) (s *Stack[E], err error)

New creates and initializes a empty stack. Options can be provided to customize the stack's properties (e.g., thread safety).

func NewFromMapKeys

func NewFromMapKeys[K comparable, V any](m map[K]V, ops ...Option[K]) (*Stack[K], error)

NewFromMapKeys creates and initializes a stack from the provided map keys. Options can be provided to customize the stack's properties (e.g., thread safety). Returns an empty stack if the provided map is nil or empty.

func NewFromMapValues

func NewFromMapValues[K comparable, V any](cmp func(V, V) int, m map[K]V, ops ...Option[V]) (*Stack[V], error)

NewFromMapValues creates a stack from the provided map values. Options can be provided to customize the stack's properties (e.g., thread safety). Returns an empty stack if the provided map is nil or empty.

func NewFromSlice

func NewFromSlice[E any](slice []E, ops ...Option[E]) (*Stack[E], error)

NewFromSlice creates and initializes a stack from the provided slice. Options can be provided to customize the stack's properties (e.g., thread safety).

func (*Stack[E]) Clear

func (s *Stack[E]) Clear()

Clear removes all elements from the stack.

func (*Stack[E]) Clone

func (s *Stack[E]) Clone() *Stack[E]

Clone returns a deep copy of the stack.

func (*Stack[E]) IsEmpty

func (s *Stack[E]) IsEmpty() bool

IsEmpty reports whether the stack has no elements.

func (*Stack[E]) Len

func (s *Stack[E]) Len() int

Len returns the number of elements currently in the stack.

func (*Stack[E]) MarshalJSON

func (s *Stack[E]) MarshalJSON() ([]byte, error)

MarshalJSON will marshal the stack into a JSON-based representation.

func (*Stack[E]) Peek

func (s *Stack[E]) Peek() (E, bool)

Peek returns the top element of the stack without removing it. Returns the zero value of E and false if the stack is empty.

func (*Stack[E]) Pop

func (s *Stack[E]) Pop() (E, bool)

Pop removes and returns the top element of the stack. Returns the zero value of E and false if the stack is empty.

func (*Stack[E]) Push

func (s *Stack[E]) Push(e E)

Push adds an element to the top of the stack. The stack's size increases by one.

func (*Stack[E]) String

func (s *Stack[E]) String() string

String returns a string representation of the stack.

func (*Stack[E]) UnmarshalJSON

func (s *Stack[E]) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON will unmarshal a JSON-based representation byte slice into the stack.

func (*Stack[E]) Values

func (s *Stack[E]) Values() []E

Values returns all elements in the stack in LIFO(last-in, first-out) order. If the stack is empty, it returns an empty slice (not nil).

Jump to

Keyboard shortcuts

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