import "github.com/474420502/structure/stack/listarray"- type Stack
- func New[T any]() *Stack[T]
- func NewWithCap[T any](cap int) *Stack[T]
- func (as *Stack[T]) Clear()
- func (as *Stack[T]) Empty() bool
- func (as *Stack[T]) Peek() (T, bool)
- func (as *Stack[T]) Pop() (T, bool)
- func (as *Stack[T]) Push(v T)
- func (as *Stack[T]) Size() uint
- func (as *Stack[T]) String() string
- func (as *Stack[T]) Values() []T
- examples
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
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() boolEmpty 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() uintSize return the size of stack
func (*Stack[T]) String
func (as *Stack[T]) String() stringString return the string of stack. a(top)->b->c
func (*Stack[T]) Values
func (as *Stack[T]) Values() []TValues return the values of stacks
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]
}