Last active
October 20, 2019 07:02
-
-
Save P-A-R-U-S/16f6495b278bf5a2d4b1a99056bf592c to your computer and use it in GitHub Desktop.
Stack implementation in Golang
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
// 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