← Back to Data Structures & Algorithms
Question 456
Build Both Ends of a Deque
Build Both Ends of a Deque
A deque has four doors: add and remove at each end. Lock two of them and the same class becomes something familiar - addFirst with removeFirst is a stack, addLast with removeFirst is a queue. LinkedDeque already gives you isEmpty, size and toString; write the four operations. Requirements: 1.addFirst(int value)andaddLast(int value)link a new DNode at that end and grow the size. 2.removeFirst()andremoveLast()unlink that end and return its value, or return -1 when the deque is empty. 3. head, tail and size must stay in agreement, so a deque drained to empty can be filled again from either end. The DNode class is provided - do not rename it and do not add public to it, because a Java source file may contain only one public class. Do NOT write a main method.
Expected Output:
3 -> 2 -> 1 -> null 3 2 1 -1 null 0 10 -> 20 -> 30 -> null 10 20 -> 30 -> null 2
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Data Structures & AlgorithmsDeques
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.