stack

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 0 Imported by: 0

README

Stack

This package provides a generic stack data structure for Go, supporting any type.

Features

  • Generic: Works with any type (Stack[T any]).
  • Push: Add an element to the top of the stack.
  • Pop: Remove and return the top element (returns zero value and false if empty).
  • Peek: View the top element without removing it.
  • IsEmpty: Check if the stack is empty.
  • Size: Get the number of elements in the stack.

Example

s := stack.New[int]()
s.Push(10)
s.Push(20)
val, ok := s.Pop() // val == 20, ok == true

Documentation

Index

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 is a generic stack data structure.

func New

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

New creates a new Stack.

func (*Stack[T]) IsEmpty

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

IsEmpty returns true if the stack is empty.

func (*Stack[T]) Peek

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

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

func (*Stack[T]) Pop

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

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

func (*Stack[T]) Push

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

Push adds an element to 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