stack

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: BSD-3-Clause Imports: 1 Imported by: 0

README

= A generics based Go Stack.

A simple stack with Push, Pop, Length, IsEmpty using Go generics.

This requires version 1.18beta1 of Go to be compiled and used.

This is a demo of how to use a generic in a type decleration and how to
delcare methods on that generic type.  Specifically look at the decleration
of `Stack[T any]` and also see how the functions are instanciated with
the generic type.

The code is a conversion of an erlier `Stack` that used `interface{}`.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyStack = errors.New("Empty Stack")

An error to indicate that the stack is empty

Functions

This section is empty.

Types

type Stack

type Stack[T any] []T

Stack is a generic type buildt on top of a slice

func (Stack[T]) IsEmpty

func (ns Stack[T]) IsEmpty() bool

IsEmpty will return true if the stack is empty

func (Stack[T]) Length

func (ns Stack[T]) Length() int

Length returns the number of elements in the stack.

func (*Stack[T]) Peek

func (ns *Stack[T]) Peek() (*T, error)

Peek returns the top element of the stack or an error indicating that the stack is empty. Some times this is refered to a 'Top'

func (*Stack[T]) Pop

func (ns *Stack[T]) Pop() (rv T, err error)

Pop will remove the top element from the stack. An error is returned if the stack is empty.

func (*Stack[T]) Push

func (ns *Stack[T]) Push(t T)

Push will push new data of type [T any] onto the stack.

func (*Stack[T]) Truncate

func (ns *Stack[T]) Truncate()

Truncate removes all data from the list.

Jump to

Keyboard shortcuts

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