Class: RubyStructures::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/rubystructures/queue.rb

Instance Method Summary collapse

Constructor Details

#initializeQueue

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

#backObject

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

#dequeueObject

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

#frontObject

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

#popObject

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

#sizeObject

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