by Pawan Kumar @jsartisan
Implement a queue data structure using stacks 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. Returnsundefined
if the queue is empty.front()
: Returns the first element of the queue without removing it. Returnsundefined
if the queue is empty.isEmpty()
: Returnstrue
if 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 QueueUsingStacks {
// Your implementation here
}
const queue = new QueueUsingStacks();
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