Created
May 16, 2022 06:14
-
-
Save sausheong/d8f43f1c2828551cb360d1d190894703 to your computer and use it in GitHub Desktop.
generics
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 StringStack struct { | |
| items []string | |
| } | |
| func (s *StringStack) Push(item string) { | |
| s.items = append(s.items, item) | |
| } | |
| func (s *StringStack) Pop() (item string) { | |
| if !s.IsEmpty() { | |
| item = s.items[len(s.items)-1] | |
| s.items = s.items[:len(s.items)-1] | |
| } | |
| return | |
| } | |
| func (s *StringStack) Peek() (item string) { | |
| if !s.IsEmpty() { | |
| item = s.items[len(s.items)-1] | |
| } | |
| return | |
| } | |
| func (s *StringStack) IsEmpty() bool { | |
| return len(s.items) == 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment