prosper/stack.go

24 lines
442 B
Go
Raw Normal View History

2021-02-14 02:09:56 +00:00
package main
import "fmt"
// Stack is a stack of integers with no defined max depth
type Stack struct {
values []int
}
// Pop returns the top of the stack
func (s *Stack) Pop() (int, error) {
if len(s.values) == 0 {
return 0, fmt.Errorf("stack empty")
}
i := s.values[0]
s.values = s.values[1:]
return i, nil
}
// Push adds a value to the top of the stack
func (s *Stack) Push(i int) {
s.values = append([]int{i}, s.values...)
}