class Containers::RubyDeque
A Deque is a container that allows items to be added and removed from both the front and back, acting as a combination of a Stack and Queue.
This implementation uses a doubly-linked list, guaranteeing O(1) complexity for all operations.
Constants
- Node
Public Class Methods
Create a new Deque. Takes an optional array argument to initialize the Deque.
d = Containers::Deque.new([1, 2, 3]) d.front #=> 1 d.back #=> 3
# File lib/containers/deque.rb 17 def initialize(ary=[]) 18 @front = nil 19 @back = nil 20 @size = 0 21 ary.to_a.each { |obj| push_back(obj) } 22 end
Public Instance Methods
Returns the object at the back of the Deque but does not remove it.
d = Containers::Deque.new d.push_front(1) d.push_front(2) d.back #=> 1
# File lib/containers/deque.rb 60 def back 61 @back && @back.obj 62 end
Removes all the objects in the Deque.
# File lib/containers/deque.rb 30 def clear 31 @front = @back = nil 32 @size = 0 33 end
Iterate over the Deque in LIFO order.
# File lib/containers/deque.rb 154 def each_backward 155 return unless @back 156 node = @back 157 while node 158 yield node.obj 159 node = node.left 160 end 161 end
Iterate over the Deque in FIFO order.
# File lib/containers/deque.rb 143 def each_forward 144 return unless @front 145 node = @front 146 while node 147 yield node.obj 148 node = node.right 149 end 150 end
Returns true if the Deque is empty, false otherwise.
# File lib/containers/deque.rb 25 def empty? 26 @size == 0 27 end
Returns the object at the front of the Deque but does not remove it.
d = Containers::Deque.new d.push_front(1) d.push_front(2) d.front #=> 2
# File lib/containers/deque.rb 50 def front 51 @front && @front.obj 52 end
Returns the object at the back of the Deque and removes it.
d = Containers::Deque.new d.push_front(1) d.push_front(2) d.pop_back #=> 1 d.size #=> 1
# File lib/containers/deque.rb 128 def pop_back 129 return nil unless @back 130 node = @back 131 if @size == 1 132 clear 133 return node.obj 134 else 135 @back.left.right = nil 136 @back = @back.left 137 end 138 @size -= 1 139 node.obj 140 end
Returns the object at the front of the Deque and removes it.
d = Containers::Deque.new d.push_front(1) d.push_front(2) d.pop_front #=> 2 d.size #=> 1
# File lib/containers/deque.rb 107 def pop_front 108 return nil unless @front 109 node = @front 110 if @size == 1 111 clear 112 return node.obj 113 else 114 @front.right.left = nil 115 @front = @front.right 116 end 117 @size -= 1 118 node.obj 119 end
Adds an object at the back of the Deque.
d = Containers::Deque.new([1, 2, 3]) d.push_back(4) d.pop_back #=> 4
# File lib/containers/deque.rb 87 def push_back(obj) 88 node = Node.new(nil, nil, obj) 89 if @back 90 node.left = @back 91 @back.right = node 92 @back = node 93 else 94 @front = @back = node 95 end 96 @size += 1 97 obj 98 end
Adds an object at the front of the Deque.
d = Containers::Deque.new([1, 2, 3]) d.push_front(0) d.pop_front #=> 0
# File lib/containers/deque.rb 69 def push_front(obj) 70 node = Node.new(nil, nil, obj) 71 if @front 72 node.right = @front 73 @front.left = node 74 @front = node 75 else 76 @front = @back = node 77 end 78 @size += 1 79 obj 80 end
Return the number of items in the Deque.
d = Containers::Deque.new([1, 2, 3]) d.size #=> 3
# File lib/containers/deque.rb 39 def size 40 @size 41 end