April 2018
Beginner to intermediate
426 pages
10h 19m
English
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 ...