Class: DataStructures::Stack
- Inherits:
-
Object
- Object
- DataStructures::Stack
- 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
- #bottom ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Stack
constructor
A new instance of Stack.
- #pop ⇒ Object
- #push(item) ⇒ Object
- #size ⇒ Object (also: #length)
- #top ⇒ Object
Constructor Details
#initialize ⇒ Stack
Returns a new instance of Stack.
6 7 8 |
# File 'lib/datastructures/stack.rb', line 6 def initialize self.clear end |
Instance Method Details
#bottom ⇒ Object
33 34 35 |
# File 'lib/datastructures/stack.rb', line 33 def bottom @array.first end |
#clear ⇒ Object
37 38 39 |
# File 'lib/datastructures/stack.rb', line 37 def clear @array = Array.new end |
#empty? ⇒ Boolean
16 17 18 |
# File 'lib/datastructures/stack.rb', line 16 def empty? @array.empty? end |
#pop ⇒ Object
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 |
#size ⇒ Object Also known as: length
10 11 12 |
# File 'lib/datastructures/stack.rb', line 10 def size @array.size end |
#top ⇒ Object
29 30 31 |
# File 'lib/datastructures/stack.rb', line 29 def top @array.last end |