Created
June 25, 2021 19:56
-
-
Save terror/5fcd8b7dae588e64b7e26a5a77f4f3bc to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import "fmt" | |
type Stack []string | |
func (s *Stack) IsEmpty() bool { | |
return len(*s) == 0 | |
} | |
func (s *Stack) Push(str string) { | |
*s = append(*s, str) | |
} | |
func (s *Stack) Pop() (string, bool) { | |
if s.IsEmpty() { | |
return "", false | |
} else { | |
idx := len(*s) - 1 | |
el := (*s)[idx] | |
*s = (*s)[:idx] | |
return el, true | |
} | |
} | |
func main() { | |
var st Stack | |
st.Push("Hello"); | |
st.Push("World"); | |
st.Push("!"); | |
if len(st) > 0 { | |
for !st.IsEmpty() { | |
x, y := st.Pop() | |
if y == true { | |
fmt.Println(x); | |
} | |
} | |
} | |
// ! | |
// World | |
// Hello | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment