Class: DataStructures::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/datastructures/stack.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeStack

Returns a new instance of Stack.



6
7
8
# File 'lib/datastructures/stack.rb', line 6

def initialize
  self.clear
end

Instance Method Details

#bottomObject



33
34
35
# File 'lib/datastructures/stack.rb', line 33

def bottom
  @array.first
end

#clearObject



37
38
39
# File 'lib/datastructures/stack.rb', line 37

def clear
  @array = Array.new
end

#empty?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/datastructures/stack.rb', line 16

def empty?
  @array.empty?
end

#popObject



24
25
26
27
# File 'lib/datastructures/stack.rb', line 24

def pop
  raise "Stack underflow: nothing to pop." if self.size == 0
  @array.pop
end

#push(item) ⇒ Object



20
21
22
# File 'lib/datastructures/stack.rb', line 20

def push item
  @array.push item
end

#sizeObject Also known as: length



10
11
12
# File 'lib/datastructures/stack.rb', line 10

def size
  @array.size
end

#topObject



29
30
31
# File 'lib/datastructures/stack.rb', line 29

def top
  @array.last
end