class DataStructures::Stack

Implements a simple FILO (first in, last out) stack data structure using an array container.

Public Class Methods

new() click to toggle source
# File lib/datastructures/stack.rb, line 6
def initialize
  self.clear
end

Public Instance Methods

bottom() click to toggle source
# File lib/datastructures/stack.rb, line 33
def bottom
  @array.first
end
clear() click to toggle source
# File lib/datastructures/stack.rb, line 37
def clear
  @array = Array.new
end
empty?() click to toggle source
# File lib/datastructures/stack.rb, line 16
def empty?
  @array.empty?
end
length()
Alias for: size
pop() click to toggle source
# File lib/datastructures/stack.rb, line 24
def pop
  raise "Stack underflow: nothing to pop." if self.size == 0
  @array.pop
end
push(item) click to toggle source
# File lib/datastructures/stack.rb, line 20
def push item
  @array.push item
end
size() click to toggle source
# File lib/datastructures/stack.rb, line 10
def size
  @array.size
end
Also aliased as: length
top() click to toggle source
# File lib/datastructures/stack.rb, line 29
def top
  @array.last
end