Added cool little generic stack
This commit is contained in:
parent
179c83ba72
commit
5f3c5e3b14
31
types/stack.go
Normal file
31
types/stack.go
Normal file
@ -0,0 +1,31 @@
|
||||
package types
|
||||
|
||||
// Stack is a generic stack data structure.
|
||||
type Stack[VALUE any] []VALUE
|
||||
|
||||
// Push adds a value to the top of the stack.
|
||||
func (stack *Stack[VALUE]) Push (value VALUE) {
|
||||
*stack = append(*stack, value)
|
||||
}
|
||||
|
||||
// Pop removes the topmost value of the stack and returns it.
|
||||
func (stack *Stack[VALUE]) Pop () (value VALUE) {
|
||||
if len(*stack) < 1 {
|
||||
panic("can't pop off of empty stack")
|
||||
}
|
||||
|
||||
newLength := len(*stack) - 1
|
||||
value = (*stack)[newLength]
|
||||
*stack = (*stack)[:newLength]
|
||||
return
|
||||
}
|
||||
|
||||
// Top returns the value on top of the stack.
|
||||
func (stack Stack[VALUE]) Top () (value VALUE) {
|
||||
if len(stack) < 1 {
|
||||
panic("can't get top of empty stack")
|
||||
}
|
||||
|
||||
value = stack[len(stack) - 1]
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user