Adding elements to the front of the deque

As we have implemented the logic for some methods already, we will focus only on the logic for the addFront method. The code for the addFront method is presented as follows:

addFront(element) {  if (this.isEmpty()) { // {1}    this.addBack(element);  } else if (this.lowestCount > 0) { // {2}    this.lowestCount--;    this.items[this.lowestCount] = element;  } else {    for (let i = this.count; i > 0; i--) { // {3}      this.items[i] = this.items[i - 1];    }    this.count++;    this.lowestCount = 0;    this.items[0] = element; // {4}  }}

When adding an element to the front of the deque, there are three scenarios. 

The first scenario is when the deque is empty ({1}). In this case, we can evoke the addBack method. The element will ...

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.