CJCoding With Joseph
15free / daySign inCreate account

Queue From Two Stacks

StackQueue must behave like a queue while owning nothing but two stacks. New values pile onto inbox; outbox holds values already flipped into arrival order. Only the two Deque fields and size() are written.

Requirements:
1. transfer(): when and only when outbox is empty, pop every value off inbox and push it onto outbox. Moving them across while outbox still holds some would serve newer values first, so that guard is the whole question.
2. isEmpty(): true only when both stacks are empty.
3. enqueue(int value): push the value onto inbox.
4. dequeue(): call transfer() first, return -1 when nothing is left, otherwise pop outbox.

Do NOT write a main method.

Expected Output:

1
4
2
3
4
5
true
-1

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & AlgorithmsQueues
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.