目录
1.结构定义
type Node struct {
Val any
Next *Node
}
type Stack struct {
Top *Node
Length int
}
2.IsEmpty()
func (stack *Stack) IsEmpty() bool {
return stack.Length == 0
}
3.len()
func (stack *Stack) len() int {
return stack.Length
}
4.Push()
func (stack *Stack) Push(data any) {
newstack := &Node{}
newstack.Val = data
newstack.Next = stack.Top
stack.Top = newstack
stack.Length++
}
5.Pop()
func (stack *Stack) Pop() any {
result := stack.Top.Val
if stack.Top.Next == nil {
stack.Top = nil
} else {
stack.Top.Val = stack.Top.Next.Val
stack.Top.Next = stack.Top.Next.Next
}
stack.Length--
return result
}
6.Peak()
func (stack *Stack) Peak() any {
return stack.Top.Val
}
7.Show()
func (stack *Stack) Show() []any {
var res []any
currrntnode := stack.Top
for currrntnode != nil {
res = append(res, currrntnode.Val)
currrntnode = currrntnode.Next
}
return res
}