2
0
mirror of https://github.com/0xAX/go-algorithms synced 2024-11-16 00:12:53 +00:00
go-algorithms/stack/stack.go
CodeLingo Bot fd089e9887 Fix function comments based on best practices from Effective Go
Signed-off-by: CodeLingo Bot <bot@codelingo.io>
2019-02-18 22:36:21 +00:00

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
}