You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-algorithms/stack/stack.go

48 lines
782 B
Go

package stack
type StackItem struct {
item interface{}
next *StackItem
}
// Stack is a base structure for LIFO
type Stack struct {
sp *StackItem
depth uint64
}
// Initialzes new Stack
func New() *Stack {
var stack *Stack = new(Stack)
stack.depth = 0
return stack
}
// Pushes a given item into Stack
func (stack *Stack) Push(item interface{}) {
stack.sp = &StackItem{item: item, next: stack.sp}
stack.depth++
}
// Deletes top of a stack and return it
func (stack *Stack) Pop() interface{} {
if stack.depth > 0 {
item := stack.sp.item
stack.sp = stack.sp.next
stack.depth--
return item
}
return nil
}
// Peek returns top of a stack without deletion
func (stack *Stack) Peek() interface{} {
if stack.depth > 0 {
return stack.sp.item
}
return nil
}