Skip to content

Latest commit

 

History

History
executable file
·
143 lines (98 loc) · 2.55 KB

File metadata and controls

executable file
·
143 lines (98 loc) · 2.55 KB

arraystack

import "github.com/474420502/structure/stack/array"

Index

type Stack

Stack the struct of stack

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

func New

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

New create a object of Stack

func (*Stack[T]) Clear

func (st *Stack[T]) Clear()

Clear Clear stack data

func (*Stack[T]) Empty

func (st *Stack[T]) Empty() bool

Empty if stack is empty, return true. else false

func (*Stack[T]) Peek

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

Peek the top of stack

func (*Stack[T]) Pop

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

Pop pop the value from stack

func (*Stack[T]) Push

func (st *Stack[T]) Push(v T)

Push Push value into stack

func (*Stack[T]) Size

func (st *Stack[T]) Size() uint

Size return the size of stack

func (*Stack[T]) String

func (st *Stack[T]) String() string

String return the string of stack

func (*Stack[T]) Values

func (st *Stack[T]) Values() []T

Values return the values of stacks

examples

package main

import (
	"log"

	arraystack "github.com/474420502/structure/stack/array"
)

func main() {

	st := arraystack.New[int]()

	log.Println("Push String Size")
	for i := 0; i < 10; i += 2 {
		st.Push(i)
	}
	log.Println(st.String()) // [0 2 4 6 8]
	log.Println(st.Size())   // 5

	log.Println("Peek Pop Empty Clear")
	log.Println(st.Peek()) // 8 true
	log.Println(st.Pop())  // 8 true
	st.Clear()
	log.Println(st.Empty()) // true
	log.Println(st.Peek())  // 0 false
	log.Println(st.Pop())   // 0 false

	log.Println("Values")
	for i := 0; i < 10; i += 2 {
		st.Push(i)
	}
	log.Println(st.Values()) // [0 2 4 6 8]
}