First commit

This commit is contained in:
2024-08-09 19:20:42 -05:00
commit 025ec775ca
14 changed files with 543 additions and 0 deletions

33
stack/stack.go Normal file
View File

@@ -0,0 +1,33 @@
// Copied from https://gist.github.com/hedhyw/d52bfdc27befe56ffc59b948086fcd9e
package stack
type Stack[T any] struct {
elements []T
}
func NewStack[T any](capacity int) *Stack[T] {
return &Stack[T]{
elements: make([]T, 0, capacity),
}
}
func (s *Stack[T]) Push(el T) {
s.elements = append(s.elements, el)
}
func (s *Stack[T]) Len() int {
return len(s.elements)
}
func (s *Stack[T]) Pop() (el T, ok bool) {
if len(s.elements) == 0 {
return el, false
}
end := len(s.elements) - 1
el = s.elements[end]
s.elements = s.elements[:end]
return el, true
}