Class: Algorithmable::DataStructs::Queue

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Errors, Enumerable
Defined in:
lib/algorithmable/data_structs/queue.rb

Constant Summary

Constants included from Errors

Errors::NoSuchElementError

Instance Method Summary collapse

Constructor Details

#initialize(collection = []) ⇒ Queue

Returns a new instance of Queue.



10
11
12
# File 'lib/algorithmable/data_structs/queue.rb', line 10

def initialize(collection = [])
  @imp = Deque.new collection
end

Instance Method Details

#dequeueObject



24
25
26
# File 'lib/algorithmable/data_structs/queue.rb', line 24

def dequeue
  @imp.pop_front
end

#enqueue(item) ⇒ Object



20
21
22
# File 'lib/algorithmable/data_structs/queue.rb', line 20

def enqueue(item)
  @imp.push_back(item)
end

#peekObject



14
15
16
17
18
# File 'lib/algorithmable/data_structs/queue.rb', line 14

def peek
  peek_value = @imp.peek_front
  fail NoSuchElementError unless peek_value
  peek_value
end