Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package stack
- type Stack struct {
- *node
- }
- type node struct {
- value string
- next *node
- }
- // New inits a new stack
- func New() *Stack {
- return &Stack{}
- }
- // Push inserts an item to the stack
- // Complexity: O(1)
- func (s *Stack) Push(v string) {
- s.node = &node{value: v, next: s.node}
- }
- // Pop removes the the last item from the stack
- // Complexity: O(1)
- func (s *Stack) Pop() string {
- n := s.node
- v := n.value
- s.node = n.next
- return v
- }
- // IsEmpty checks the stack's emptiness
- // Complexity: O(1)
- func (s *Stack) IsEmpty() bool {
- return s.node == nil
- }
- // Size returns the size of the stack
- // Complexity: O(n)
- func (s *Stack) Size() uint {
- if s.IsEmpty() {
- return 0
- }
- size := uint(1)
- t := s.node
- for t.next != nil {
- size++
- t = t.next
- }
- return size
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement