← Back to Data Structures & Algorithms
Question 443
Queue From Two Stacks
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 andsize()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(): calltransfer()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
Sign in to use hintsTopics:
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.