Class Containers::Queue

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

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. <<
  2. each
  3. empty?
  4. next
  5. pop
  6. push
  7. size

Included modules

  1. Enumerable

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
17:   def initialize(ary=[])
18:     @container = Containers::Deque.new(ary)
19:   end

Public instance methods

<< (obj)

Alias for push

each (&block)

Iterate over the Queue in FIFO order.

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

Returns true if the queue is empty, false otherwise.

[show source]
    # File lib/containers/queue.rb, line 59
59:   def empty?
60:     @container.empty?
61:   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
26:   def next
27:     @container.front
28:   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
46:   def pop
47:     @container.pop_front
48:   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
36:   def push(obj)
37:     @container.push_back(obj)
38:   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
54:   def size
55:     @container.size
56:   end