A similar question was asked earlier there, but the question here is the reverse of it, using two queues as a stack. The question...
Given two queues with their sta
Here is some simple pseudo code, push is O(n), pop / peek is O(1):
Qpush = Qinstance()
Qpop = Qinstance()
def stack.push(item):
Qpush.add(item)
while Qpop.peek() != null: //transfer Qpop into Qpush
Qpush.add(Qpop.remove())
swap = Qpush
Qpush = Qpop
Qpop = swap
def stack.pop():
return Qpop.remove()
def stack.peek():
return Qpop.peek()