Sha256: 7d070721802e08132b1d546f56fa17917b553cce6845edf9d6b33e27e05d98d7

Contents?: true

Size: 965 Bytes

Versions: 1

Compression:

Stored size: 965 Bytes

Contents

# A StackQueue works just like a queue, but it's based on
# a Stack.  Functionality-wise, it's exactly the same
# as a queue.  I mainly made it as an exercise leading
# up to MinMaxStackQueue.
module Zadt
  class StackQueue
    def initialize
      @in = Stack.new
      @out = Stack.new
    end

    def self.help
      puts "Here are the functions for StackQueue:"
      puts "show"
      puts "enqueue(value)"
      puts "dequeue"
      puts "length"
      puts "empty?"
    end

    def self.methods
      self.help
    end

    def help
      StackQueue.help
    end

    def methods
      help
    end

    def show
      @out.show.reverse + @in.show
    end

    def enqueue(val)
      @in.push(val)
    end

    def dequeue
      if @out.empty?
        @in.length.times do
          @out.push(@in.pop)
        end
      end
      @out.pop
    end

    def length
      @in.length + @out.length
    end

    def empty?
      @values.empty?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zadt-0.1.1 lib/zadt/AbstractDataTypes/StackQueue.rb