Sha256: 83093db7af3ed46376c759f928d5d40237db9f6e8561fc1dd8a632995eae7bbb

Contents?: true

Size: 891 Bytes

Versions: 1

Compression:

Stored size: 891 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 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

1 entries across 1 versions & 1 rubygems

Version Path
zadt-1.1.7 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStack.rb