Skip to content

Instantly share code, notes, and snippets.

@P-A-R-U-S
Last active October 20, 2019 07:02
Show Gist options
  • Save P-A-R-U-S/16f6495b278bf5a2d4b1a99056bf592c to your computer and use it in GitHub Desktop.
Save P-A-R-U-S/16f6495b278bf5a2d4b1a99056bf592c to your computer and use it in GitHub Desktop.
Stack implementation in Golang
// Following implemetation of Stack tie each element to it parent
type stackElement struct {
data interface{}
next *stackElement
}
type Stack struct {
Size int32
head *stackElement
}
func (stack *Stack) Push(data interface{}) {
element := new(stackElement)
element.data = data
element.next = stack.head
stack.head = element
stack.Size++
}
func (stack *Stack) Pop() interface{} {
if stack.head == nil {
return nil
}
v := stack.head.data
stack.head = stack.head.next
stack.Size--
return v
}
func (stack *Stack) IsEmpty() bool {
return stack.head == nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment