Creating the Deque class
As usual, we will start by declaring the Deque
class located in the file src/05-queue-deque/deque.js
:
class Deque {
#items = [];
}
We will continue using an array-based implementation for our data structure. And given the deque data structure allows inserting and removing from both ends, we will have the following methods:
addFront(item)
: This method adds a new element at the front of the deque.addRear(item)
: This method adds a new element at the back of the deque.removeFront()
: This method removes the first element from the deque.removeRear()
: This method removes the last element from the deque.peekFront()
: This method returns the first element from the deque.peekRear()
: This method returns the last element from the deque.
The
...Deque
class also implements theisEmpty
,clear
,size
, andtoString
methods (you can check the complete source code by downloading the source code bundle for this book). The code for these is the same as for theQueue
class.