596.Implement a Queue Using an Array
Easy
Implement a queue and run a list of operations on it.
ops[i] is one of "enqueue" (add args[i] at the back), "dequeue" (remove and report the front item, or −1 if empty), "front" (report the front item without removing it, or −1), "isEmpty" (report 1 or 0) and "size". Return everything reported, in order — "enqueue" reports nothing.
A queueQueueA first-in, first-out (FIFO) collection: items join at the back and leave from the front. works like a line of people: the first to arrive is the first served. Storing it in an array raises a practical question — what happens to the empty slot at the front after a dequeue? Shifting everything left is easy but slow; moving a head indexHead IndexAn index that marks where the front of a queue currently is inside its array. instead makes every operation O(1).
Example 1:
Input: ops = ["enqueue","enqueue","dequeue","front","size"], args = [5,8,0,0,0]
Output: [5,8,1]
Example 2:
Input: ops = ["dequeue","isEmpty"], args = [0,0]
Output: [-1,1]
+ 3 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ ops.length ≤ 1000 - ●
ops[i] is one of "enqueue", "dequeue", "front", "isEmpty", "size" - ●
args[i] is the value for "enqueue" and 0 otherwise
ops =
["enqueue", "enqueue", "dequeue", "front", "size"]
args =
[5, 8, 0, 0, 0]