Skip to content

Latest commit

 

History

History
executable file
·
152 lines (104 loc) · 2.78 KB

File metadata and controls

executable file
·
152 lines (104 loc) · 2.78 KB

lastack

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

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 NewWithCap[T any](cap int) *Stack[T]

New create a object of stack with the capacity

func (*Stack[T]) Clear

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

Clear Clear stack data

func (*Stack[T]) Empty

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

Empty if stack is empty, return true. else false

func (*Stack[T]) Peek

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

Peek the top of stack

func (*Stack[T]) Pop

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

Pop pop the value from stack

func (*Stack[T]) Push

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

Push Push value into stack

func (*Stack[T]) Size

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

Size return the size of stack

func (*Stack[T]) String

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

String return the string of stack. a(top)->b->c

func (*Stack[T]) Values

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

Values return the values of stacks

examples

package main

import (
	"log"

	lastack "github.com/474420502/structure/stack/listarray"
)

func main() {

	st := lastack.NewWithCap[int](16)

	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]
}