Skip to content

Instantly share code, notes, and snippets.

@scbizu
Created May 31, 2016 03:40
Show Gist options
  • Save scbizu/d2888e1cf6fd180258b320651284c6b7 to your computer and use it in GitHub Desktop.
Save scbizu/d2888e1cf6fd180258b320651284c6b7 to your computer and use it in GitHub Desktop.
LIFO stack golang
//Stack Golang
package main
import "fmt"
type Element struct {
next *Element
val interface{}
}
type Stack struct {
top *Element
size int
}
func (stack *Stack) Len() int {
return stack.size
}
func (stack *Stack) push(val interface{}) {
stack.top = &Element{stack.top, val}
stack.size++
}
func (stack *Stack) pop() interface{} {
if stack.size > 0 {
value := stack.top.val
stack.top = stack.top.next
stack.size--
return value
} else {
return nil
}
}
func main() {
stack := new(Stack)
stack.push(123)
stack.push("nace")
stack.push(1.23)
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