Class: Algorithmable::DataStructs::Stack
- Inherits:
-
Object
- Object
- Algorithmable::DataStructs::Stack
show all
- Extended by:
- Forwardable
- Includes:
- Errors
- Defined in:
- lib/algorithmable/data_structs/stack.rb
Constant Summary
Constants included
from Errors
Errors::NoSuchElementError
Instance Method Summary
collapse
Constructor Details
#initialize(collection = []) ⇒ Stack
Returns a new instance of Stack.
9
10
11
12
|
# File 'lib/algorithmable/data_structs/stack.rb', line 9
def initialize(collection = [])
@imp = Deque.new
collection.each { |item| @imp.push_front item }
end
|
Instance Method Details
#peek ⇒ Object
14
15
16
17
18
|
# File 'lib/algorithmable/data_structs/stack.rb', line 14
def peek
peek_value = @imp.peek_front
fail NoSuchElementError unless peek_value
peek_value
end
|
#pop ⇒ Object
24
25
26
|
# File 'lib/algorithmable/data_structs/stack.rb', line 24
def pop
@imp.pop_front
end
|
#push(item) ⇒ Object
20
21
22
|
# File 'lib/algorithmable/data_structs/stack.rb', line 20
def push(item)
@imp.push_front(item)
end
|