Stack

Stack

A stack is a linear data structure that supports adding elements to one end and removing elements from that same end.

OperationBig-O
Top/PeekO(1)
PushO(1)
PopO(1)
SearchO(n)
isEmptyO(1)

Examples

go

For short lived stacks

var stack []string
// Push
stack = append(stack, "Hello")
// Peek
fmt.Print(stack[len(stack)-1])
// Pop
stack = stack[:len(stack)-1]

For long lived stacks

stack := list.New()
// Push
stack.PushBack("Hello")
// Peek
stack.Back()
// Pop
stack.Remove(stack.Back())

References