Class: RubyStructures::Queue
- Inherits:
-
Object
- Object
- RubyStructures::Queue
- Defined in:
- lib/rubystructures/queue.rb
Instance Method Summary collapse
-
#back ⇒ Object
Public: Returns the last value added to the Queue.
-
#dequeue ⇒ Object
Alias for Queue.pop.
-
#empty? ⇒ Boolean
Public: Checks if whether or not the Queue is empty.
-
#enqueue(value) ⇒ Object
Alias for Queue.push(value).
-
#front ⇒ Object
Public: Returns the next value to be served by the Queue without removing the value from the Queue.
-
#initialize ⇒ Queue
constructor
Public: Creates a new instance of Queue.
-
#pop ⇒ Object
Public: Returns and removes the next item in the Queue.
-
#push(value) ⇒ Object
Public: Adds a value to the Queue.
-
#size ⇒ Object
Public: Returns the size of the Queue.
Constructor Details
#initialize ⇒ Queue
Public: Creates a new instance of Queue.
Examples
@queue = Queue.new # => Queue
Returns a new instance of Queue.
11 12 13 |
# File 'lib/rubystructures/queue.rb', line 11 def initialize @storage = [] end |
Instance Method Details
#back ⇒ Object
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.
80 81 82 |
# File 'lib/rubystructures/queue.rb', line 80 def back @storage.first end |
#dequeue ⇒ Object
Alias for Queue.pop
Examples
@queue.dequeue # => 42
112 113 114 |
# File 'lib/rubystructures/queue.rb', line 112 def dequeue self.pop end |
#empty? ⇒ Boolean
Public: Checks if whether or not the Queue is empty.
Examples
@queue.empty? # => true
Returns true if the Queue is empty; false otherwise.
92 93 94 |
# File 'lib/rubystructures/queue.rb', line 92 def empty? @storage.size == 0 end |
#enqueue(value) ⇒ Object
Alias for Queue.push(value)
Examples
@queue.push(42) # => true
102 103 104 |
# File 'lib/rubystructures/queue.rb', line 102 def enqueue(value) self.push value end |
#front ⇒ Object
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.
68 69 70 |
# File 'lib/rubystructures/queue.rb', line 68 def front @storage.last end |
#pop ⇒ Object
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.
55 56 57 |
# File 'lib/rubystructures/queue.rb', line 55 def pop @storage.pop end |
#push(value) ⇒ Object
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.
38 39 40 41 42 43 44 |
# File 'lib/rubystructures/queue.rb', line 38 def push(value) if @storage.insert(0, value) true else false end end |
#size ⇒ Object
Public: Returns the size of the Queue.
Examples
@queue.size # => 1
Returns the Integer size of the Queue.
23 24 25 |
# File 'lib/rubystructures/queue.rb', line 23 def size @storage.size end |