Sha256: 65886cd1aab68a2786dee0ae60e42c3ed9baa9edda2a9cc76431dc3508ac9301

Contents?: true

Size: 919 Bytes

Versions: 8

Compression:

Stored size: 919 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
      Zadt::ADT::show_stackqueue_help_message
    end

    def help
      StackQueue.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 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

8 entries across 8 versions & 1 rubygems

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