Skip to content

Instantly share code, notes, and snippets.

@t11a
Created January 7, 2014 12:36
Show Gist options
  • Save t11a/8298695 to your computer and use it in GitHub Desktop.
Save t11a/8298695 to your computer and use it in GitHub Desktop.
class StackWithSingleQueue
def initialize
@queue = []
end
def push(item)
@queue << item
end
def pop()
size = @queue.size
(size-1).times do
@queue << @queue.shift
end
@queue.shift
end
end
stack = StackWithSingleQueue.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