1 Star 0 Fork 0

jackytse / tabtoy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
stack.go 1012 Bytes
一键复制 编辑 原始数据 按行查看 历史
黑色灵猫 提交于 2018-04-09 16:22 . 添加: 表达式计算
package exprvm
import (
"fmt"
"strings"
)
type node struct {
value interface{}
prev *node
}
type Stack struct {
top *node
length int
}
func (self *Stack) String() string {
var sb strings.Builder
index := 0
for i := self.top; i != nil; i = i.prev {
sb.WriteString(fmt.Sprintf("[%d] %v\n", index, i.value))
index++
}
return sb.String()
}
// Create a new stack
func NewStack() *Stack {
return &Stack{nil, 0}
}
// Return the number of items in the stack
func (this *Stack) Len() int {
return this.length
}
// View the top item on the stack
func (this *Stack) Peek() interface{} {
if this.length == 0 {
return nil
}
return this.top.value
}
// Pop the top item of the stack and return it
func (this *Stack) Pop() interface{} {
if this.length == 0 {
return nil
}
n := this.top
this.top = n.prev
this.length--
return n.value
}
// Push a value onto the top of the stack
func (this *Stack) Push(value interface{}) {
n := &node{value, this.top}
this.top = n
this.length++
}
Go
1
https://gitee.com/jackytse/tabtoy.git
git@gitee.com:jackytse/tabtoy.git
jackytse
tabtoy
tabtoy
v0.1.0

搜索帮助