← Back to Data Structures & Algorithms
Question 442
Build Both Ends of a Linked Queue
Build Both Ends of a Linked Queue
LinkedQueue adds at the tail and removes at the head, so the two missing methods must keep head, tail and size telling the same story. Requirements: 1.enqueue(int value): link a new Node on at the tail, point head and tail at it when the queue was empty, and grow size. 2.dequeue(): return -1 when empty, otherwise savehead.value, move head on, shrink size and return the saved value. 3. Whendequeue()removes the last node, tail must go back to null - otherwise the next enqueue hangs off a node that has left the queue and the value is lost. The Node 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:
10 -> 20 -> 30 -> null 10 20 30 null 0 40 -> 50 -> null 40 2
= 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.