Class: Containers::Stack

### Home Classes Methods

### In Files

- lib/containers/stack.rb

### Parent

Object

### Methods

- ::new
- #<<
- #each
- #empty?
- #next
- #pop
- #push
- #size

### Included Modules

- Enumerable

### Files

- Makefile
- Makefile
- Makefile
- Makefile
- Makefile

### Class/Module Index

Quicksearch

- Containers
- Containers::CBst
- Containers::CDeque
- Containers::CRBTreeMap
- Containers::CSplayTreeMap
- Containers::Heap
- Containers::KDTree
- Containers::MaxHeap
- Containers::MinHeap
- Containers::PriorityQueue
- Containers::Queue
- Containers::RubyDeque
- Containers::RubyDeque
- Containers::RubyRBTreeMap
- Containers::RubyRBTreeMap
- Containers::RubySplayTreeMap
- Containers::RubySplayTreeMap
- Containers::Stack
- Containers::SuffixArray
- Containers::Trie
- Object
- Object::Deque
- Object::RBTreeMap
- Object::SplayTreeMap
- Algorithms
- Algorithms::Search
- Algorithms::String

No matching classes.

# Containers::Stack

A Stack is a container that keeps elements in a last-in first-out (LIFO) order. There are many uses for stacks, including prefix-infix-postfix conversion and backtracking problems.

This implementation uses a doubly-linked list, guaranteeing O(1) complexity for all operations.

### Public Class Methods

new(ary=[]) click to toggle source

Create a new stack. Takes an optional array argument to initialize the stack.

    s = Containers::Stack.new([1, 2, 3])
    s.pop #=> 3
    s.pop #=> 2

    # File lib/containers/stack.rb, line 16
    def initialize(ary=[])
      @container = Containers::Deque.new(ary)
    end

### Public Instance Methods

<<(obj) click to toggle source
Alias for: push
each(&block) click to toggle source

Iterate over the Stack in LIFO order.

    # File lib/containers/stack.rb, line 63
    def each(&block)
      @container.each_backward(&block)
    end

empty?() click to toggle source

Returns true if the stack is empty, false otherwise.

    # File lib/containers/stack.rb, line 58
    def empty?
      @container.empty?
    end

next() click to toggle source

Returns the next item from the stack but does not remove it.

    s = Containers::Stack.new([1, 2, 3])
    s.next #=> 3
    s.size #=> 3

    # File lib/containers/stack.rb, line 25
    def next
      @container.back
    end

pop() click to toggle source

Removes the next item from the stack and returns it.

    s = Containers::Stack.new([1, 2, 3])
    s.pop #=> 3
    s.size #=> 2

    # File lib/containers/stack.rb, line 45
    def pop
      @container.pop_back
    end

push(obj) click to toggle source

Adds an item to the stack.

    s = Containers::Stack.new([1])
    s.push(2)
    s.pop #=> 2
    s.pop #=> 1

    # File lib/containers/stack.rb, line 35
    def push(obj)
      @container.push_back(obj)
    end

Also aliased as: <<
size() click to toggle source

Return the number of items in the stack.

    s = Containers::Stack.new([1, 2, 3])
    s.size #=> 3

    # File lib/containers/stack.rb, line 53
    def size
      @container.size
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.