Created
April 12, 2018 02:40
-
-
Save Ch4s3/93c72a43f1360da25404fa6f2de81eef to your computer and use it in GitHub Desktop.
a simple two stack based queue in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
([email protected]).each { @outbox << @inbox.pop } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment