class Containers::Queue

  1. lib/containers/queue.rb
Parent: Containers

A Queue is a container that keeps elements in a first-in first-out (FIFO) order. Because of its properties, it is often used as a buffer.

This implementation uses a doubly-linked list, guaranteeing O(1) complexity for all operations.

Methods

Public Class

  1. new

Public Instance

  1. each
  2. empty?
  3. next
  4. pop
  5. push
  6. size

Included modules

  1. Enumerable

Public Instance Aliases

<< -> push

Public Class methods

new (ary=[])

Create a new queue. Takes an optional array argument to initialize the queue.

q = Containers::Queue.new([1, 2, 3])
q.pop #=> 1
q.pop #=> 2
[show source]
# File lib/containers/queue.rb, line 17
def initialize(ary=[])
  @container = Containers::Deque.new(ary)
end

Public Instance methods

each (&block)

Iterate over the Queue in FIFO order.

[show source]
# File lib/containers/queue.rb, line 64
def each(&block)
  @container.each_forward(&block)
end
empty? ()

Returns true if the queue is empty, false otherwise.

[show source]
# File lib/containers/queue.rb, line 59
def empty?
  @container.empty?
end
next ()

Returns the next item from the queue but does not remove it.

q = Containers::Queue.new([1, 2, 3])
q.next #=> 1
q.size #=> 3
[show source]
# File lib/containers/queue.rb, line 26
def next
  @container.front
end
pop ()

Removes the next item from the queue and returns it.

q = Containers::Queue.new([1, 2, 3])
q.pop #=> 1
q.size #=> 2
[show source]
# File lib/containers/queue.rb, line 46
def pop
  @container.pop_front
end
push (obj)

Adds an item to the queue.

q = Containers::Queue.new([1])
q.push(2)
q.pop #=> 1
q.pop #=> 2
[show source]
# File lib/containers/queue.rb, line 36
def push(obj)
  @container.push_back(obj)
end
size ()

Return the number of items in the queue.

q = Containers::Queue.new([1, 2, 3])
q.size #=> 3
[show source]
# File lib/containers/queue.rb, line 54
def size
  @container.size
end