Class: RubyDataStructures::QueueAsArray
- Inherits:
-
Object
- Object
- RubyDataStructures::QueueAsArray
- Defined in:
- lib/RubyDataStructures/queue_as_array.rb
Instance Method Summary collapse
-
#dequeue ⇒ Object
The queue is dequeued.
-
#empty? ⇒ Boolean
Returns
true
if the queue is empty. -
#enqueue(element) ⇒ Object
The queue is enqueued with
element
. -
#full? ⇒ Boolean
Returns
true
if the queue is full. -
#initialize(size = 1) ⇒ QueueAsArray
constructor
Initializes a stack of size
size
The value ofhead
for a new stack isnil
The value oftail
for a new stack is0
. -
#reset ⇒ Object
Resets the queue.
Constructor Details
#initialize(size = 1) ⇒ QueueAsArray
Initializes a stack of size size
The value of head
for a new stack is nil
The value of tail
for a new stack is 0
5 6 7 8 |
# File 'lib/RubyDataStructures/queue_as_array.rb', line 5 def initialize(size = 1) @length = size self.reset end |
Instance Method Details
#dequeue ⇒ Object
The queue is dequeued
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/RubyDataStructures/queue_as_array.rb', line 38 def dequeue raise "Queue Underflow - The queue is empty" if self.empty? x = @array[@head] if @head == @length - 1 @head = 0 else @head = @head + 1 end if @head == @tail self.reset end return x end |
#empty? ⇒ Boolean
Returns true
if the queue is empty
11 12 13 |
# File 'lib/RubyDataStructures/queue_as_array.rb', line 11 def empty? @head.nil? end |
#enqueue(element) ⇒ Object
The queue is enqueued with element
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/RubyDataStructures/queue_as_array.rb', line 21 def enqueue(element) raise "Queue Overflow - The queue is full" if self.full? @array[@tail] = element if @head.nil? @head = 0 end if @tail == @length - 1 @tail = 0 else @tail = @tail + 1 end end |
#full? ⇒ Boolean
Returns true
if the queue is full
16 17 18 |
# File 'lib/RubyDataStructures/queue_as_array.rb', line 16 def full? @head == @tail end |
#reset ⇒ Object
Resets the queue
57 58 59 60 61 |
# File 'lib/RubyDataStructures/queue_as_array.rb', line 57 def reset @array = Array.new(@length) @head = nil @tail = 0 end |