This post will cover the basics of a queue along with a few helper methods. You’ll quickly notice queues are very similar to stacks. However, the methodology is completely different. Queues are often used with linked lists, which will be covered in another article. As with stacks, queues are linear in structure. They follow a first in first out approach. This means that the first element put into the list is the first element out. This is a very common structure you see in everyday life, for example a line in a grocery store.
Efficiency. The majority of array methods have a time complexity of o(n) whereas queues can remain entirely at o(1). (This is dependant on the helper methods you would like to add). As well as with stacks these data structures help you manage your data. They are less flexible than arrays which will help you manage your data.
The core methods of a queue are “enqueue”, which will add, and “dequeue”, which will remove. Some common helper methods I’ve chosen to include are: toString, first, last and isEmpty.
class Queue {
constructor(elements) {
// Provided array or empty array
this.elements = elements || [];
// Provided array length or zero
this.size = (elements && elements.length) || 0;
}
// Enqueue the element to the tail and increment
enqueue(element) {
this.size++;
this.elements.unshift(element);
}
// Returns null if the array is empty otherwise
// decrement and remove / return the element
// from the head
dequeue() {
if (this.size === 0)
return null;
this.size--;
return this.elements.pop();
}
// If array is not empty return the head element
first() {
return Boolean(this.size)
? this.elements[this.size -1]
: null;
}
// If array is not empty return the tail element
last() {
return Boolean(this.size)
? this.elements[0]
: null;
}
// Returns a boolean based on array size
isEmpty() {
return this.size === 0;
}
// Returns the contents of the list
toString() {
return this.elements.reduce((a, e) => a += `${e}\n`, '')
}
}
const queue = new Queue();
// Enqueue Andrew
queue.enqueue('Andrew');
// Enqueue Jersome
queue.enqueue('Jerome');
// Enqueue Kishan
queue.enqueue('Kishan');
// Returns Andrew
console.log(queue.first());
// Returns and removes Andrew
console.log(queue.dequeue());
// Returns false
console.log(queue.isEmpty());
// Return Kishan
console.log(queue.last());
// Returns
// Kishan
// Jerome
console.log(queue.toString());
A queue is a more efficient data structure than simply using an array and helps manage data.