Sha256: 67e018b18522ae1b1b4620b58664b78e908002d10163fec0418a2d815003ab82

Contents?: true

Size: 802 Bytes

Versions: 1

Compression:

Stored size: 802 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 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 peek
      if @out.empty?
        @in.length.times do
          @out.push(@in.pop)
        end
      end
      @out.peek
    end

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

    def empty?
      @in.empty? && @out.empty?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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