Created
June 13, 2018 15:39
-
-
Save lkrych/117d990e3227cb293b29941783f1a0d4 to your computer and use it in GitHub Desktop.
Stack implementation in Go
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
type stack struct { //use a struct to define the stack | |
data []int | |
} | |
func (s *stack) pop() int { | |
popped := s.data[len(s.data)-1] | |
s.data = s.data[:len(s.data)-1] //resize the internal slice | |
return popped | |
} | |
func (s *stack) push(item int) { | |
s.data = append(s.data, item) | |
} | |
func (s *stack) peek() int { | |
return s.data[len(s.data)-1] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment