stack

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2020 License: MIT Imports: 1 Imported by: 0

README

stack

import "go.ajitem.com/stack"

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.

Usage

var IsEmpty = errors.New("stack is empty")
type Stack
type Stack struct {
}
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

Example

package main

import (
    "fmt"
    "go.ajitem.com/stack"
)

func main() {
    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
}

Documentation

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
package main

import (
	"fmt"
	"go.ajitem.com/stack"
)

func main() {
	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

Constants

This section is empty.

Variables

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

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

Functions

This section is empty.

Types

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

Jump to

Keyboard shortcuts

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