Create a PseudoQueue class without using an existing queue. The class should implement a standard queue interface using the methods below but will internally utilize only two Stack objects.
Use only push
, pop
, and peek
methods of the Stack
instances.
The class should have the following methods.
enqueue(value)
- Insertsvalue
into the PseudoQueue using a FIFO approach.dequeue()
- Extracts a value from the PseudoQueue, using a FIFO approach.
In order to get FIFO behavior from FILO/LIFO stacks, we need to move
all values from one internal stack to the other whenever a switch is made from enqueuing to dequeuing. We can think of the two stacks as
inStack
and outStack
.
- move all values from
outStack
toinStack
- while
outStack.peek()
is notnull
inStack.push(outStack.pop())
- while
inStack.push(value)
- move all values from
inStack
tooutStack
- while
inStack.peek()
is notnull
outStack.push(inStack.pop())
- while
- return value of
outStack.pop()