by Pawan Kumar @jsartisan
Implement a queue data structure in JavaScript. A queue follows the First-In-First-Out (FIFO) principle. Your queue should have the following methods:
enqueue(element): Adds an element to the end of the queue.dequeue(): Removes the first element from the queue and returns it. Returnsundefinedif the queue is empty.front(): Returns the first element of the queue without removing it. Returnsundefinedif the queue is empty.isEmpty(): Returnstrueif the queue is empty, otherwise returnsfalse.size(): Returns the number of elements in the queue.
Use the following example to understand how the queue should work:
class Queue {
// Your implementation here
}
const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
console.log(queue.front()); // 1
console.log(queue.dequeue()); // 1
console.log(queue.size()); // 1
console.log(queue.isEmpty()); // false
queue.dequeue();
console.log(queue.isEmpty()); // true