Class: DS::SimpleQueue
- Inherits:
-
Object
- Object
- DS::SimpleQueue
- Defined in:
- lib/ds/queues/simple_queue.rb
Overview
Class implements queue data structure.
Class Method Summary collapse
-
.create(*args) ⇒ Object
Create new queue.
Instance Method Summary collapse
-
#dequeue ⇒ Object
(also: #shift)
Removes element from queue and returns it.
-
#empty? ⇒ Boolean
Checks if queue is empty.
-
#enqueue(x) ⇒ Object
(also: #push)
Adds element to queue and returns queue itself.
-
#initialize(*args) ⇒ SimpleQueue
constructor
Create new queue.
-
#length ⇒ Object
(also: #size)
Returns length of queue.
-
#peek ⇒ Object
Returns element from forehead of queue.
Constructor Details
#initialize(*args) ⇒ SimpleQueue
Create new queue. First parameter determines how the queue will be represented internally.
6 7 8 9 10 11 12 |
# File 'lib/ds/queues/simple_queue.rb', line 6 def initialize(*args) @store = if args.first.is_a? List args.first else args || [] end end |
Class Method Details
Instance Method Details
#dequeue ⇒ Object Also known as: shift
Removes element from queue and returns it.
28 29 30 |
# File 'lib/ds/queues/simple_queue.rb', line 28 def dequeue store.shift end |
#empty? ⇒ Boolean
Checks if queue is empty.
47 48 49 |
# File 'lib/ds/queues/simple_queue.rb', line 47 def empty? store.empty? end |
#enqueue(x) ⇒ Object Also known as: push
Adds element to queue and returns queue itself.
20 21 22 23 |
# File 'lib/ds/queues/simple_queue.rb', line 20 def enqueue(x) store << x self end |
#length ⇒ Object Also known as: size
Returns length of queue. If queue is empty returns 0.
40 41 42 |
# File 'lib/ds/queues/simple_queue.rb', line 40 def length store.length end |
#peek ⇒ Object
Returns element from forehead of queue.
35 36 37 |
# File 'lib/ds/queues/simple_queue.rb', line 35 def peek store.first end |