class RubyStructures::Queue

Public Class Methods

new() click to toggle source

Public: Creates a new instance of Queue.

Examples

@queue = Queue.new
# => Queue

Returns a new instance of Queue.

# File lib/rubystructures/queue.rb, line 11
def initialize
        @storage = []
end

Public Instance Methods

back() click to toggle source

Public: Returns the last value added to the Queue.

Examples

@queue.back
# => 42

Returns the value at the end of the Queue, which would be served last.

# File lib/rubystructures/queue.rb, line 80
def back
        @storage.first
end
dequeue() click to toggle source

Alias for Queue.pop

Examples

@queue.dequeue
# => 42
# File lib/rubystructures/queue.rb, line 112
def dequeue
        self.pop
end
empty?() click to toggle source

Public: Checks if whether or not the Queue is empty.

Examples

@queue.empty?
# => true

Returns true if the Queue is empty; false otherwise.

# File lib/rubystructures/queue.rb, line 92
def empty?
        @storage.size == 0
end
enqueue(value) click to toggle source

Alias for Queue.push(value)

Examples

@queue.push(42)
# => true
# File lib/rubystructures/queue.rb, line 102
def enqueue(value)
        self.push value
end
front() click to toggle source

Public: Returns the next value to be served by the Queue without removing the value from the Queue.

Examples

@queue.front
# => 42

Returns the value of the next item to be served in the Queue.

# File lib/rubystructures/queue.rb, line 68
def front
        @storage.last
end
pop() click to toggle source

Public: Returns and removes the next item in the Queue.

Examples

@queue.pop
# => 42

Returns the next value to be served by the Queue, unless the Queue is empty, then returns nil.

# File lib/rubystructures/queue.rb, line 55
def pop
        @storage.pop
end
push(value) click to toggle source

Public: Adds a value to the Queue.

value - Ruby data type, can be of any class type.

Examples

@queue.push(42)
# => true

Returns true if the value is successfully added to the Queue; returns false otherwise.

# File lib/rubystructures/queue.rb, line 38
def push(value)
        if @storage.insert(0, value)
                true
        else
                false
        end
end
size() click to toggle source

Public: Returns the size of the Queue.

Examples

@queue.size
# => 1

Returns the Integer size of the Queue.

# File lib/rubystructures/queue.rb, line 23
def size
        @storage.size
end