Class: DSA::Deque
Overview
DSA::Deque - Double-ended queue data structure.
Supports efficient insertion and removal from both ends. Includes Enumerable for functional-style operations.
Instance Method Summary collapse
-
#back ⇒ Object
Returns the value at the back of the deque without removing it.
-
#each {|val| ... } ⇒ Enumerator
Yields each element in the deque from front to back.
-
#empty? ⇒ Boolean
Checks if the deque is empty.
-
#front ⇒ Object
Returns the value at the front of the deque without removing it.
-
#initialize ⇒ DSA::Deque
constructor
Initialize a new empty deque.
-
#pop_back ⇒ Object
Removes and returns the value at the back of the deque.
-
#pop_front ⇒ Object
Removes and returns the value at the front of the deque.
-
#push_back(val) ⇒ DSA::Deque
Inserts a value at the back of the deque.
-
#push_front(val) ⇒ DSA::Deque
Inserts a value at the front of the deque.
-
#size ⇒ Integer
Returns the number of elements in the deque.
-
#to_a ⇒ Array
Returns a defensive copy of the deque as an array.
Constructor Details
#initialize ⇒ DSA::Deque
Initialize a new empty deque.
19 20 21 22 |
# File 'lib/dsa-ruby/deque.rb', line 19 def initialize @deque = [] @head = 0 end |
Instance Method Details
#back ⇒ Object
Returns the value at the back of the deque without removing it.
89 90 91 92 |
# File 'lib/dsa-ruby/deque.rb', line 89 def back raise IndexError, "deque is empty" if empty? @deque.last end |
#each {|val| ... } ⇒ Enumerator
Yields each element in the deque from front to back.
134 135 136 137 |
# File 'lib/dsa-ruby/deque.rb', line 134 def each(&block) return enum_for(:each) unless block_given? @deque.each(&block) end |
#empty? ⇒ Boolean
Checks if the deque is empty.
110 111 112 |
# File 'lib/dsa-ruby/deque.rb', line 110 def empty? @deque.empty? end |
#front ⇒ Object
Returns the value at the front of the deque without removing it.
77 78 79 80 |
# File 'lib/dsa-ruby/deque.rb', line 77 def front raise IndexError, "deque is empty" if empty? @deque.first end |
#pop_back ⇒ Object
Removes and returns the value at the back of the deque.
65 66 67 68 |
# File 'lib/dsa-ruby/deque.rb', line 65 def pop_back raise IndexError, "deque is empty" if empty? @deque.pop end |
#pop_front ⇒ Object
Removes and returns the value at the front of the deque.
53 54 55 56 |
# File 'lib/dsa-ruby/deque.rb', line 53 def pop_front raise IndexError, "deque is empty" if empty? @deque.shift end |
#push_back(val) ⇒ DSA::Deque
Inserts a value at the back of the deque.
41 42 43 44 |
# File 'lib/dsa-ruby/deque.rb', line 41 def push_back(val) @deque.push(val) self end |
#push_front(val) ⇒ DSA::Deque
Inserts a value at the front of the deque.
30 31 32 33 |
# File 'lib/dsa-ruby/deque.rb', line 30 def push_front(val) @deque.unshift(val) self end |
#size ⇒ Integer
Returns the number of elements in the deque.
100 101 102 |
# File 'lib/dsa-ruby/deque.rb', line 100 def size @deque.size end |
#to_a ⇒ Array
Returns a defensive copy of the deque as an array.
120 121 122 |
# File 'lib/dsa-ruby/deque.rb', line 120 def to_a @deque.dup end |