Class: Algorithmable::Cups::StacksAndQueues::TwoStacksQueue
- Inherits:
-
Object
- Object
- Algorithmable::Cups::StacksAndQueues::TwoStacksQueue
show all
- Includes:
- DataStructs
- Defined in:
- lib/algorithmable/cups/stacks_and_queues/two_stacks_queue.rb
Instance Method Summary
collapse
#new_bag, #new_deque_queue, #new_fifo_queue, #new_lifo_queue, #new_max_priority_queue, #new_min_priority_queue, #new_ordered_symbol_table, #new_priority_queue
#new_ordered_binary_tree
#new_doubly_linked_list, #new_singly_linked_list
Constructor Details
Returns a new instance of TwoStacksQueue.
7
8
9
10
|
# File 'lib/algorithmable/cups/stacks_and_queues/two_stacks_queue.rb', line 7
def initialize
@stack_a = new_lifo_queue
@stack_b = new_lifo_queue
end
|
Instance Method Details
#dequeue ⇒ Object
23
24
25
26
27
|
# File 'lib/algorithmable/cups/stacks_and_queues/two_stacks_queue.rb', line 23
def dequeue
return @stack_b.dequeue unless @stack_b.empty?
@stack_b.push @stack_a.pop until @stack_a.empty?
@stack_b.pop
end
|
#enqueue(item) ⇒ Object
16
17
18
19
20
21
|
# File 'lib/algorithmable/cups/stacks_and_queues/two_stacks_queue.rb', line 16
def enqueue(item)
unless @stack_b.empty?
@stack_a.push @stack_b.pop until @stack_b.empty?
end
@stack_a.push(item)
end
|
#peek ⇒ Object
29
30
31
32
33
|
# File 'lib/algorithmable/cups/stacks_and_queues/two_stacks_queue.rb', line 29
def peek
return @stack_b.peek unless @stack_b.empty?
@stack_b.push @stack_a.pop until @stack_a.empty?
@stack_b.peek
end
|
#size ⇒ Object
12
13
14
|
# File 'lib/algorithmable/cups/stacks_and_queues/two_stacks_queue.rb', line 12
def size
@stack_a.size + @stack_b.size
end
|