Back to godoc.org
go.ajitem.com/stack/v2

package stack

v2.0.2
Latest Go to latest
Published: Apr 26, 2020 | License: MIT | Module: go.ajitem.com/stack/v2

Overview

A stack is a fundamental data structure which is extensively used. This package provides a basic implementation of a stack in Go. A stack works using Last In First Out (LIFO) method.

Example

Code:

s := stack.New()

s.Push(1)
s.Push(2)
s.Push(3)
s.Push(4)

fmt.Println("size:", s.Size())

number, _ := s.Pop()
fmt.Println("number:", number)
number, _ = s.Pop()
fmt.Println("number:", number)
number, _ = s.Pop()
fmt.Println("number:", number)

fmt.Println("size:", s.Size())
number, _ = s.Peek()
fmt.Println("number:", number)

Output:

size: 4
number: 4
number: 3
number: 2
size: 1
number: 1

Index

Examples

Variables

var IsEmpty = errors.New("stack is empty")

IsEmpty is the error returned by the stack when no elements are present in the stack

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

func New

func New() *Stack

Create a new stack

func (*Stack) Peek

func (s *Stack) Peek() (int, error)

Return the item on the top of the stack

func (*Stack) Pop

func (s *Stack) Pop() (int, error)

Pop (delete) the item on the top of stack and return it

func (*Stack) Push

func (s *Stack) Push(number int)

Push (add) a new value to the top of the stack

func (*Stack) Size

func (s *Stack) Size() int

Returns the size (number of elements) in the stack

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier