Created
March 20, 2022 08:09
-
-
Save kougazhang/a86411c6bbc06d6465bf2cc8d4582b86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
与 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