class Containers::Stack

rdoc

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.

MIT License

Copyright (c) 2009 Kanwei Li

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

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 37
def initialize(ary=[])
  @container = Containers::Deque.new(ary)
end

Public Instance Methods

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

Iterate over the Stack in LIFO order.

# File lib/containers/stack.rb, line 84
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 79
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 46
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 66
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 56
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 74
def size
  @container.size
end