Skip to content

Instantly share code, notes, and snippets.

@t11a
Created January 7, 2014 12:36
Show Gist options
  • Save t11a/8298703 to your computer and use it in GitHub Desktop.
Save t11a/8298703 to your computer and use it in GitHub Desktop.
class StackWithTwoQueues
def initialize
@q1 = []
@q2 = []
end
def push(item)
@q1 << item
end
def pop()
while true
head = @q1.shift
break if @q1.empty?
@q2 << head
end
tmp = @q1
@q1 = @q2
@q2 = tmp
head
end
end
stack = StackWithTwoQueues.new
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
p stack.pop() # => 4
p stack.pop() # => 3
stack.push(5)
p stack.pop() # => 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment