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.
a simple two stack based queue in ruby
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment