Deque

What is Deque?

In computer science, a double-ended queue (Deque) is an abstract data type that generalizes a Queue, for which elements can be added to or removed from either the front (head) or back (tail) - Wikipedia

List Of Operations Available

  • AddFront : Insert an element at the front of the Deque.

  • AddBack : Insert an element at the back of the Deque.

  • RemoveFront : Remove an element from front.

  • RemoveBack : Remove an element from the back.

  • PeekBack : This method returns the first element of the Deque same as queue frontarrow-up-right method.

  • PeekFront : This method returns the end element of the Deque same as stack peekarrow-up-right method.

  • Size : Return Size of the deque.

  • isEmpty : Check if the Deque is empty if empty return true else false.

  • Clear : Reset the Deque.

Implementation of Deque in Javascript

Deque class is similar to queuearrow-up-right.

AddBack

Deque addback method is similar to queue's enqueuearrow-up-right method.

AddFront

When adding an element at the front Deque, There are three scenarios, 1. If the Deque is Empty then same as addBack method ({1}) 2. When an element is removed from the front of the Deque ({2}),lowestCount will be greater > zero,

  • Then decrement the count

  • Assign the element to that object key.

    1. If the lowestCount is equal to zero then, we need to shift the element by one position right and free the first position and assign the element to that object key ({3})

For removing an element from the Deque, we first need to check, if the Deque is not Empty else return undefined.

RemoveFront

While an element from the front of the Deque is as dequeuearrow-up-right method of Queue

RemoveBack

While an element from the back of the Deque is as poparrow-up-right method of the Stack

sizearrow-up-right,cleararrow-up-right,isEmptyarrow-up-right will be same as queue methods

you get the full source herearrow-up-right

Conclusion :

Methods

Complexity

addback

O(1)

addfront

O(1)

removeFront

O(1)

removeBack

O(1)

Last updated