Program
Underlying data-structure is array, this class will just restrict the user to access directly by index etc.
// Queue class
class Queue
{
// Array is used to implement a Queue
constructor()
{
this.items = [];
}
enqueue(element)
{
// adding element to the queue
this.items.push(element);
}
dequeue()
{
// removing element from the queue
if(this.isEmpty())
throw new Error("Underflow");
return this.items.shift(); // pop but from front
}
// front function
peek()
{
// returns the Front element of
// the queue without removing it.
if(this.isEmpty())
return "No elements in Queue";
return this.items[0];
}
isEmpty() {
return this.items.length === 0;
}
// printQueue()
}