Skip to content

Instantly share code, notes, and snippets.

@maksadbek
Created April 10, 2020 18:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
package main
import (
"fmt"
)
type item struct {
value interface{} //value as interface type to hold any data type
next *item
}
type Stack struct {
top *item
size int
}
func (stack *Stack) Len() int {
return stack.size
}
func (stack *Stack) Push(value interface{}) {
stack.top = &item{
value: value,
next: stack.top,
}
stack.size++
}
func (stack *Stack) Pop() (value interface{}) {
if stack.Len() > 0 {
value = stack.top.value
stack.top = stack.top.next
stack.size--
return
}
return nil
}
func main() {
stack := new(Stack)
// Push different data type to the stack
stack.Push(1)
stack.Push("Welcome")
stack.Push(4.0)
// Pop until stack is empty
for stack.Len() > 0 {
fmt.Println(stack.Pop())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment