Data Structures

Double Ended Queue (Deque) in Data structure

Double Ended Queue (Deque) is a linear data structure that allows insertion and deletion at both sides. So, it is a combination of both Stack and Queue data structures.

1. Representation

Double Ended Queue (Deque) in Data structure
Double Ended Queue (Deque) in Data structure
  • Front: Front side of the queue
  • Rear: Rear side of the queue
  • Element: The actual data

2. Types of Deque

  • Input Restricted Queue – Insertion is restricted to one end while deletion is allowed at both ends.
  • Output Restricted Queue – Deletion is restricted to one end while insertion is allowed at both ends.

3. Implementations of Deque

Double Ended Queues can be implemented using Circular Arrays and Doubly Linked Lists. However, choose the correct implementation based on your requirements.

4. Deque Operations and Time Complexity

OperationShort DescriptionTime
Complexity
Space
Complexity
Insert FrontInsert a new element at the Front sideO (1) O (n)
Insert RearInsert a new element at the Rear sideO (1) O (n)
Delete FrontDelete an element from the FrontO (1)O (n)
Delete RearDelete an element from the RearO (1)O (n)
SearchSearch the element by valueO (n)O (n)
Time and Space complexity of Double Ended Queue (Deque) operations

5. Advantages and Disadvantages

Advantages

  • Deque has the ability to perform both LIFO (Last-In First-Out) and FIFO (First-In First-Out) operations with the same data. So this can be used as a combination of both Stacks and Queues.
  • Also, it is efficient to work with both ends in O(1) time complexity.

Disadvantages

  • Not suitable for sorting and searching operations.

6. Applications

When to Use Double Ended Queues

Deque is required for the data that requires access/modifications from both ends. Also, it is useful in multithreading to access the data concurrently between threads.

Realtime Applications

  • Steal job scheduling algorithm in a multiprocessor system. Each processor has its own Deque and processes the jobs from the Front side. If a processor finishes all of its jobs, then it can steal from the back of another processor’s Deque.
  • Web browser’s history is another example of Deque. The new pages are inserted at the Front while the old ones are removed from the Rear. Also, it has the ability to retrieve the last visited pages by retrieving them from the Front.
Copyright copyright 2024 Simplerize