Class: Algorithmable::Cups::StacksAndQueues::StackWithMin

Inherits:
DataStructs::Stack show all
Defined in:
lib/algorithmable/cups/stacks_and_queues/stack_with_min.rb

Constant Summary collapse

MAX_INT =
2**32

Constants included from Errors

Errors::NoSuchElementError

Instance Method Summary collapse

Methods inherited from DataStructs::Stack

#initialize, #peek, #pop, #to_s

Constructor Details

This class inherits a constructor from Algorithmable::DataStructs::Stack

Instance Method Details

#minObject


16
17
18
19
# File 'lib/algorithmable/cups/stacks_and_queues/stack_with_min.rb', line 16

def min
  return MAX_INT if empty?
  peek.min
end

#push(item) ⇒ Object


10
11
12
13
14
# File 'lib/algorithmable/cups/stacks_and_queues/stack_with_min.rb', line 10

def push(item)
  new_min = [item, min].min
  node = NodeWithMin.new item, new_min
  super node
end