Class: Algorithmable::DataStructs::Stack

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Errors
Defined in:
lib/algorithmable/data_structs/stack.rb

Direct Known Subclasses

Cups::StacksAndQueues::StackWithMin

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

#peekObject


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

#popObject


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

#to_sObject


28
29
30
# File 'lib/algorithmable/data_structs/stack.rb', line 28

def to_s
  to_a.join '->'
end