Stacks

A stack that represents a last-in-first-out (LIFO) data structure. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack.

Implements Container interface.

  1. type Stack interface {
  2. Push(value interface{})
  3. Pop() (value interface{}, ok bool)
  4. Peek() (value interface{}, ok bool)
  5. containers.Container
  6. // Empty() bool
  7. // Size() int
  8. // Clear()
  9. // Values() []interface{}
  10. }

LinkedListStack

A stack based on a linked list.

Implements Stack, IteratorWithIndex, JSONSerializer and JSONDeserializer interfaces.

  1. package main
  2. import lls "github.com/emirpasic/gods/stacks/linkedliststack"
  3. func main() {
  4. stack := lls.New() // empty
  5. stack.Push(1) // 1
  6. stack.Push(2) // 1, 2
  7. stack.Values() // 2, 1 (LIFO order)
  8. _, _ = stack.Peek() // 2,true
  9. _, _ = stack.Pop() // 2, true
  10. _, _ = stack.Pop() // 1, true
  11. _, _ = stack.Pop() // nil, false (nothing to pop)
  12. stack.Push(1) // 1
  13. stack.Clear() // empty
  14. stack.Empty() // true
  15. stack.Size() // 0
  16. }

ArrayStack

A stack based on a array list.

Implements Stack, IteratorWithIndex, JSONSerializer and JSONDeserializer interfaces.

  1. package main
  2. import "github.com/emirpasic/gods/stacks/arraystack"
  3. func main() {
  4. stack := arraystack.New() // empty
  5. stack.Push(1) // 1
  6. stack.Push(2) // 1, 2
  7. stack.Values() // 2, 1 (LIFO order)
  8. _, _ = stack.Peek() // 2,true
  9. _, _ = stack.Pop() // 2, true
  10. _, _ = stack.Pop() // 1, true
  11. _, _ = stack.Pop() // nil, false (nothing to pop)
  12. stack.Push(1) // 1
  13. stack.Clear() // empty
  14. stack.Empty() // true
  15. stack.Size() // 0
  16. }