代码拉取完成,页面将自动刷新
package compute
import (
"errors"
)
type StringStack struct {
Slice []string
Pos int
}
type FloatStack struct {
Slice []float64
Pos int
}
func NewStringStack() *StringStack {
return &StringStack{
Slice: []string{},
Pos: -1,
}
}
func NewFloatStack() *FloatStack {
return &FloatStack{
Slice: []float64{},
Pos: -1,
}
}
func (s *StringStack) Push(a string) {
s.Pos++
if s.Pos < len(s.Slice) {
s.Slice[s.Pos] = a
} else {
s.Slice = append(s.Slice, a)
}
}
func (s *StringStack) Pop() (string, error) {
ret, err := s.Top()
if err != nil {
return "", errors.New("Can't pop; stack is empty!")
}
s.Pos--
return ret, nil
}
func (s *StringStack) SafePop() string {
ret, _ := s.Pop()
return ret
}
func (s *StringStack) Top() (string, error) {
if s.Pos < 0 {
return "", errors.New("No elements in stack!")
}
return s.Slice[s.Pos], nil
}
func (s *StringStack) SafeTop() string {
ret, _ := s.Top()
return ret
}
func (s *FloatStack) Push(a float64) {
s.Pos++
if s.Pos < len(s.Slice) {
s.Slice[s.Pos] = a
} else {
s.Slice = append(s.Slice, a)
}
}
func (s *FloatStack) Pop() (float64, error) {
ret, err := s.Top()
if err != nil {
return 0, errors.New("Can't pop; stack is empty!")
}
s.Pos--
return ret, nil
}
func (s *FloatStack) Top() (float64, error) {
if s.Pos < 0 {
return 0, errors.New("No elements in stack!")
}
return s.Slice[s.Pos], nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。