Sha256: f9eaab60610c8d76f8457d9ef910c977a0ad7a65229940995b4e75dab4d78d6c

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

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 ZagorskiADT
  class MinMaxStack
    def initialize
      @values = []
    end

    def self.help
      puts "Here are the functions for MinMaxStack:"
      puts "push(value)"
      puts "pop"
      puts "peek"
      puts "min"
      puts "max"
      puts "length"
      puts "show"
      puts "empty?"
    end

    def self.methods
      self.help
    end

    def help
      MinMaxStack.help
    end

    def methods
      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

2 entries across 2 versions & 1 rubygems

Version Path
ZagorskiADT-0.1.4 lib/ZagorskiADT/AbstractDataTypes/MinMaxStack.rb
ZagorskiADT-0.1.3 lib/ZagorskiADT/AbstractDataTypes/MinMaxStack.rb