Skip to content

Instantly share code, notes, and snippets.

@Ch4s3
Created April 12, 2018 02:40
Show Gist options
  • Save Ch4s3/93c72a43f1360da25404fa6f2de81eef to your computer and use it in GitHub Desktop.
Save Ch4s3/93c72a43f1360da25404fa6f2de81eef to your computer and use it in GitHub Desktop.

Revisions

  1. Ch4s3 created this gist Apr 12, 2018.
    27 changes: 27 additions & 0 deletions double_stack.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    class DoubleStack
    attr_accessor :inbox, :outbox
    def initialize
    @inbox = []
    @outbox = []
    end

    def enqueue(item)
    @inbox << item
    end

    def dequeue
    transfer if @outbox.empty?
    @outbox.pop
    end

    def front
    transfer if @outbox.empty?
    @outbox.last
    end

    private

    def transfer
    (1..@inbox.length).each { @outbox << @inbox.pop }
    end
    end