Skip to content

Instantly share code, notes, and snippets.

@Gitart
Forked from oludouglas/stack.go
Created December 24, 2021 14:00
Show Gist options
  • Save Gitart/68419521e6c1453caf080ae402ee85c8 to your computer and use it in GitHub Desktop.
Save Gitart/68419521e6c1453caf080ae402ee85c8 to your computer and use it in GitHub Desktop.
type Stack struct {
store []interface{}
}
func (s *Stack) IsEmpty() bool {
return len(s.store) == 0
}
func (s *Stack) Size() int {
return len(s.store)
}
func (s *Stack) Push(item interface{}) {
s.store = append(s.store, item)
}
func (s *Stack) Peek() interface{} {
if s.IsEmpty() {
return nil
}
return s.store[len(s.store)-1]
}
func (s *Stack) Pop() interface{} {
if s.IsEmpty() {
return nil
}
v := s.store[len(s.store)-1]
s.store = s.store[:len(s.store)-1]
return v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment