diff options
-rw-r--r-- | stack.go | 14 |
1 files changed, 8 insertions, 6 deletions
@@ -21,24 +21,26 @@ func (s *Stack) Insert(v interface{}) { // Pop value from tail func (s *Stack) Pop() (v interface{}) { - if size := len(*s); size > 0 { - v, *s = (*s)[size-1], (*s)[:size-1] + if sp := len(*s) - 1; sp >= 0 { + v, *s = (*s)[sp], (*s)[:sp] } return v } // Peek a top value on Stack func (s Stack) Peek() (v interface{}) { - if size := len(s); size > 0 { - v = s[size-1] + if sp := len(s) - 1; sp >= 0 { + v = s[sp] } return v } // Swap two top values func (s Stack) Swap() { - if size := len(s); size >= 2 { - s[size-1], s[size-2] = s[size-2], s[size-1] + if sp := len(s); sp >= 2 { + a := sp - 1 + b := sp - 2 + s[a], s[b] = s[b], s[a] } } |