Skip to content

Instantly share code, notes, and snippets.

@oludouglas
Created September 15, 2020 12:48
Show Gist options
  • Save oludouglas/01bddb3740b85568e761cac5a3d638b5 to your computer and use it in GitHub Desktop.
Save oludouglas/01bddb3740b85568e761cac5a3d638b5 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