stack

package
v0.7.12 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2022 License: MIT, MIT Imports: 0 Imported by: 0

README

stack

import "github.com/fufuok/utils/generic/stack"

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

Example

package main

import (
	"fmt"
	"github.com/fufuok/utils/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

Documentation

Overview

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

Example
package main

import (
	"fmt"

	"github.com/fufuok/utils/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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stack

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

Stack implements a LIFO stack with peeking.

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.

Jump to

Keyboard shortcuts

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