Skip to content

Latest commit

 

History

History
 
 

stack

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

stack

import "github.com/zyedidia/generic/stack"

Package stack provides an implementation of a LIFO stack built using a resizing array.

Example

package main

import (
	"fmt"

	"github.com/zyedidia/generic/stack"
)

func main() {
	st := stack.New[string]()
	st.Push("foo")
	st.Push("bar")

	fmt.Println(st.Pop())
	fmt.Println(st.Peek())

	st.Push("baz")
	fmt.Println(st.Size())
}

Output

bar
foo
2

Index

type Stack

Stack implements a LIFO stack with peeking.

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

func New

func New[T any]() *Stack[T]

New returns an empty stack.

func (*Stack[T]) Copy

func (s *Stack[T]) Copy() *Stack[T]

Copy returns a copy of this stack.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (t T)

Peek returns the stack's top element but does not remove it. If the stack is empty the zero value is returned.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (t T)

Pop removes the stack's top element and returns it. If the stack is empty it returns the zero value.

func (*Stack[T]) Push

func (s *Stack[T]) Push(value T)

Push places 'value' at the top of the stack.

func (*Stack[T]) Size

func (s *Stack[T]) Size() int

Size returns the number of elements in the stack.

Generated by gomarkdoc