020-004-008
Switching Between Stack and Queue with ArrayDeque
Medium
Problem Description
Switching Between Stack and Queue with ArrayDeque
In this problem, you will create methods that receive a comma-separated string of elements and use ArrayDeque to reorder them in either stack (LIFO) or queue (FIFO) order.
Learning Objective: Understand how to use Deque as both a stack (LIFO) and a queue (FIFO)
Overview
Create methods using ArrayDeque to demonstrate the difference between stack and queue operations on the same data.
Specifications
stackOrder(String elements) method
- Receives a comma-separated element string (e.g.,
"A,B,C") - Adds each element using
push()and retrieves all usingpop() - Returns elements joined by newlines (LIFO order)
queueOrder(String elements) method
- Receives a comma-separated element string (e.g.,
"A,B,C") - Adds each element using
add()and retrieves all usingpoll() - Returns elements joined by newlines (FIFO order)
Example
stackOrder("A,B,C") → "C\nB\nA"
queueOrder("A,B,C") → "A\nB\nC"
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up