stack

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package stack is a simple implementation of the stack data structure.

Example (BasicUsage)
package main

import (
	"fmt"

	"github.com/eng618/go-eng/ds/stack"
)

func main() {
	s := stack.New()

	s.Push(25)
	s.Push(1)
	s.Push(2)

	if v, err := s.Pop(); err == nil {
		fmt.Println("Pop returned", v)
	}
}
Output:

Pop returned 2
Example (SeededList)
package main

import (
	"fmt"

	"github.com/eng618/go-eng/ds/stack"
)

func main() {
	s := stack.NewSeeded([]stack.Item{1, 2, 3, 4, 5})

	if v, err := s.Pop(); err == nil {
		fmt.Println("Pop returned", v)
	}
}
Output:

Pop returned 5

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item int

Item is the type a stack accepts.

type Stack added in v0.5.0

type Stack struct {
	Items []Item
	// contains filtered or unexported fields
}

Stack is the structure to interact with the stack package.

func New

func New() *Stack

New creates an empty stack to be pushed too.

func NewSeeded

func NewSeeded(xi []Item) *Stack

NewSeeded creates a new stack with a seeded list xi.

func (*Stack) Pop added in v0.5.0

func (s *Stack) Pop() (i Item, err error)

Pop returns the last item placed on the stack. LIFO. Returns -1 if there is nothing on the list to return.

func (*Stack) Push added in v0.5.0

func (s *Stack) Push(i Item) (ok bool)

Push adds the provided item to the stack.

Jump to

Keyboard shortcuts

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