Implement a Queue class supporting the standard FIFO (first-in, first-out) operations. New items enter at the back and the oldest item leaves from the front, mirroring a line of people at a counter.
The class must support these methods:
- enqueue(value): add value to the back of the queue
- dequeue(): remove and return the front value, or undefined if empty
- peek(): return the front value without removing it, or undefined if empty
- size(): return the number of items currently in the queue
- isEmpty(): return true if the queue has no items, false otherwise
Input
A sequence of operation names with their arguments, applied to a single Queue instance.
Output
A list of return values, one per operation. The constructor returns null.
Examples
Example 1
Input["Queue", "enqueue", "enqueue", "peek", "size", "dequeue", "isEmpty"]
[[], [10], [20], [], [], [], []]
Output[null, null, null, 10, 2, 10, false]
Explanation: After enqueuing 10 then 20, peek shows 10, size is 2, dequeue removes and returns 10, and the queue is not empty.
Example 2
Input["Queue", "isEmpty", "dequeue", "size"]
[[], [], [], []]
Output[null, true, null, 0]
Explanation: A new queue is empty, dequeue on empty returns undefined (null), and size is 0.
Constraints
All operations should run in O(1) amortized time.
Dequeue and peek on an empty queue must not throw.