Skip to content

Instantly share code, notes, and snippets.

@kougazhang
Created March 20, 2022 08:09
Show Gist options
  • Save kougazhang/a86411c6bbc06d6465bf2cc8d4582b86 to your computer and use it in GitHub Desktop.
Save kougazhang/a86411c6bbc06d6465bf2cc8d4582b86 to your computer and use it in GitHub Desktop.
func newStackLink() *StackLink {
return &StackLink{
list: list.New(),
}
}
type StackLink struct {
list *list.List
}
func (s *StackLink) Push(a byte) {
s.list.PushBack(a)
}
func (s *StackLink) Pop() byte {
v := s.list.Back()
s.list.Remove(v)
return v.Value.(byte)
}
func (s *StackLink) Top() byte {
return s.list.Back().Value.(byte)
}
func (s *StackLink) IsEmpty() bool {
return s.list.Len() == 0
}
@kougazhang
Copy link
Author

与 array 相比,linklist 实现的 stack 效率更低:

// BenchmarkParse-12 492105 2838 ns/op 4928 B/op 69 allocs/op
// BenchmarkParse-12 359936 3657 ns/op 6208 B/op 95 allocs/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment