24 lines
354 B
Go
24 lines
354 B
Go
package containers
|
|
|
|
type Stack[T any] struct {
|
|
data []T
|
|
}
|
|
|
|
func (s *Stack[T]) Push(t T) {
|
|
s.data = append(s.data, t)
|
|
}
|
|
|
|
func (s *Stack[T]) Pop() (T, bool) {
|
|
if len(s.data) == 0 {
|
|
var t T
|
|
return t, false
|
|
}
|
|
res := s.data[len(s.data)-1]
|
|
s.data = s.data[:len(s.data)-1]
|
|
return res, true
|
|
}
|
|
|
|
func NewStack[T any]() *Stack[T] {
|
|
return &Stack[T]{}
|
|
}
|