Sha256: cf0e6eee85f1ab5ae10e98f65e69f03a3645ff4533cd8f95c97b3467a4dcace6

Contents?: true

Size: 1010 Bytes

Versions: 8

Compression:

Stored size: 1010 Bytes

Contents

# A MinMaxStack is a stack that allows for Min and Max
# to be found in constant time, unlike a stack or
# array that generally takes linear time.
module Zadt
  class MinMaxStack
    def initialize
      @values = []
    end

    def self.help
      Zadt::ADT::show_minmaxstack_help_message
    end

    def help
      MinMaxStack.help
    end

    def push(val)
      if @values.empty?
        @values.push([val, val, val])
      else
        cur_min = @values.last[1]
        cur_max = @values.last[2]
        @values.push([val, [val, cur_min].min, [val, cur_max].max])
        @values
      end
    end

    def pop
      @values.pop[0]
    end

    def peek
      @values.last[0]
    end

    def min
      @values.empty? ? "Empty" : @values.last[1]
    end

    def max
      @values.empty? ? "Empty" : @values.last[2]
    end

    def length
      @values.length
    end

    def show
      showthis = []
      @values.map{|value| value[0]}
    end

    def empty?
      @values.empty?
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
zadt-1.1.6 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStack.rb
zadt-1.1.5 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStack.rb
zadt-1.1.4 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStack.rb
zadt-1.1.3 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStack.rb
zadt-1.1.2 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStack.rb
zadt-1.1.1 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStack.rb
zadt-1.1.0 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStack.rb
zadt-0.1.9 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStack.rb