2019-05-08 02:42:51 +00:00
|
|
|
package lastack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Node struct {
|
|
|
|
value interface{}
|
|
|
|
down *Node
|
|
|
|
}
|
|
|
|
|
|
|
|
type Stack struct {
|
|
|
|
top *Node
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Stack {
|
|
|
|
s := &Stack{}
|
|
|
|
s.size = 0
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *Stack) Clear() {
|
|
|
|
as.size = 0
|
|
|
|
as.top = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *Stack) Empty() bool {
|
|
|
|
return as.size == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *Stack) Size() int {
|
|
|
|
return as.size
|
|
|
|
}
|
|
|
|
|
2019-07-19 19:55:45 +00:00
|
|
|
// String 从左到右 左边第一个表示Top 如链表 a(top)->b->c
|
2019-05-08 02:42:51 +00:00
|
|
|
func (as *Stack) String() string {
|
|
|
|
content := ""
|
|
|
|
cur := as.top
|
|
|
|
for ; cur != nil; cur = cur.down {
|
|
|
|
content += spew.Sprint(cur.value) + " "
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(content) > 0 {
|
|
|
|
content = content[0 : len(content)-1]
|
|
|
|
} else {
|
|
|
|
content = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *Stack) Values() []interface{} {
|
2019-07-19 19:55:45 +00:00
|
|
|
|
|
|
|
if as.size == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-08 02:42:51 +00:00
|
|
|
result := make([]interface{}, as.size, as.size)
|
|
|
|
|
|
|
|
cur := as.top
|
|
|
|
n := 0
|
|
|
|
for ; cur != nil; cur = cur.down {
|
|
|
|
result[n] = cur.value
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *Stack) Push(v interface{}) {
|
|
|
|
nv := &Node{value: v}
|
|
|
|
nv.down = as.top
|
|
|
|
as.top = nv
|
|
|
|
as.size++
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *Stack) Pop() (interface{}, bool) {
|
|
|
|
if as.size <= 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
as.size--
|
|
|
|
|
|
|
|
result := as.top
|
|
|
|
as.top = as.top.down
|
|
|
|
result.down = nil
|
|
|
|
return result.value, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (as *Stack) Peek() (interface{}, bool) {
|
|
|
|
if as.size <= 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return as.top.value, true
|
|
|
|
}
|