Class: Algorithmable::DataStructs::Queue

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Errors
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


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

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

Instance Method Details

#dequeueObject


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

def dequeue
  @imp.pop_front
end

#enqueue(item) ⇒ Object


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

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

#peekObject


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

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

#to_sObject


27
28
29
# File 'lib/algorithmable/data_structs/queue.rb', line 27

def to_s
  to_a.join('->')
end