Class: Algorithmable::DataStructs::Stack

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Errors, Enumerable
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.



10
11
12
13
# File 'lib/algorithmable/data_structs/stack.rb', line 10

def initialize(collection = [])
  @imp = Deque.new
  collection.each { |item| @imp.push_front item }
end

Instance Method Details

#peekObject



15
16
17
18
19
# File 'lib/algorithmable/data_structs/stack.rb', line 15

def peek
  peek_value = @imp.peek_front
  fail NoSuchElementError unless peek_value
  peek_value
end

#popObject



25
26
27
# File 'lib/algorithmable/data_structs/stack.rb', line 25

def pop
  @imp.pop_front
end

#push(item) ⇒ Object



21
22
23
# File 'lib/algorithmable/data_structs/stack.rb', line 21

def push(item)
  @imp.push_front(item)
end