Enqueuing elements to the queue

The first method that we will implement is the enqueue method. This method will be responsible for adding new elements to the queue, with one very important detail: we can only add new elements to the end of the queue:

enqueue(element) {  this.items[this.count] = element;  this.count++;} 

The enqueue method has the same implementation as the push method from the Stack class. As the items property is a JavaScript object, it is a collection of key and value pairs. To add an element to the queue, we will use the count variable as the key of the items object and the element will be its value. After pushing the element to the stack, we increment the count

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.