Class: DataStructures::Queue
- Inherits:
-
Object
- Object
- DataStructures::Queue
- Defined in:
- lib/datastructures/queue.rb
Overview
Implements a simple FIFO (first in, first out) queue data structure using an array container.
Instance Method Summary collapse
- #back ⇒ Object
- #clear ⇒ Object
- #dequeue ⇒ Object
- #empty? ⇒ Boolean
- #enqueue(item) ⇒ Object
- #front ⇒ Object
-
#initialize ⇒ Queue
constructor
A new instance of Queue.
- #size ⇒ Object (also: #length)
Constructor Details
#initialize ⇒ Queue
Returns a new instance of Queue.
6 7 8 |
# File 'lib/datastructures/queue.rb', line 6 def initialize self.clear end |
Instance Method Details
#back ⇒ Object
33 34 35 |
# File 'lib/datastructures/queue.rb', line 33 def back @array.last end |
#clear ⇒ Object
37 38 39 |
# File 'lib/datastructures/queue.rb', line 37 def clear @array = Array.new end |
#dequeue ⇒ Object
24 25 26 27 |
# File 'lib/datastructures/queue.rb', line 24 def dequeue raise "Queue underflow: nothing to dequeue." if self.size == 0 @array.shift end |
#empty? ⇒ Boolean
16 17 18 |
# File 'lib/datastructures/queue.rb', line 16 def empty? @array.empty? end |
#enqueue(item) ⇒ Object
20 21 22 |
# File 'lib/datastructures/queue.rb', line 20 def enqueue item @array.push item end |
#front ⇒ Object
29 30 31 |
# File 'lib/datastructures/queue.rb', line 29 def front @array.first end |
#size ⇒ Object Also known as: length
10 11 12 |
# File 'lib/datastructures/queue.rb', line 10 def size @array.size end |