Queue Implementation

Easy~15 min

Implement a MyQueue class that represents a first-in, first-out (FIFO) queue of integers.

The MyQueue class should support the following operations:

  • MyQueue() — Initialize an empty queue.
  • enqueue(val) — Add the integer val to the back of the queue.
  • dequeue() — Remove and return the element at the front of the queue. Return -1 if the queue is empty.
  • front() — Return the element at the front of the queue without removing it. Return -1 if the queue is empty.
  • isEmpty() — Return true if the queue is empty, false otherwise.
  • size() — Return the number of elements in the queue.

Examples

Example 1
Input: ["MyQueue","enqueue","enqueue","enqueue","front","dequeue","size","isEmpty"] [[],[1],[2],[3],[],[],[],[]]
Output: [null,null,null,null,1,1,2,false]
Explanation: MyQueue q = new MyQueue(); q.enqueue(1); // queue: [1] q.enqueue(2); // queue: [1, 2] q.enqueue(3); // queue: [1, 2, 3] q.front(); // return 1 (front element) q.dequeue(); // return 1, queue: [2, 3] q.size(); // return 2 q.isEmpty(); // return false

Constraints

  • -1000 <= val <= 1000
  • At most 1000 calls will be made to enqueue, dequeue, front, isEmpty, and size
  • dequeue and front return -1 when the queue is empty
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run