![]() |
VOOZH | about |
A Deque (Double-Ended Queue) is a data structure that allows elements to be added or removed from both ends—front and rear. Unlike a regular queue, which only allows insertions at the rear and deletions from the front, a deque provides flexibility by enabling both operations at either end. This makes it ideal for scenarios where you need fast access and modification of data from both sides, such as in scheduling, buffering, and palindrome checking etc.
Deques can be implemented using arrays or linked lists, and they support operations like insertFront, insertRear, deleteFront and deleteRear efficiently.
In a Simple Array Implementation of Deque, an array is used to store elements. The deque allows insertion and deletion from both ends, but the main challenge is managing the shifting of elements when performing operations. In this implementation, the front and rear pointers are used to track the insertion and deletion positions. The array has a fixed size, and resizing is required once the deque is full.
This approach can be inefficient because shifting elements can take linear time in some cases, and resizing the array can lead to additional overhead.
Key points:
Please Refer Array implementation of Deque – Simple for implementation
| Operations | Time Complexity |
|---|---|
| insertFront(data) | O(n) (due to shifting elements) |
| insertRear(data) | O(1) (if no resizing) |
| deleteFront() | O(n) (due to shifting elements) |
| deleteRear() | O(1) |
| getFront() | O(1) |
| getRear() | O(1) |
| isEmpty() | O(1) |
| size() | O(1) |
| erase() | O(n) |
A Circular Array Implementation of Deque addresses the limitations of a simple array by allowing the array to "wrap around" when it reaches the end. Instead of shifting elements when performing insertions or deletions, the array's front and rear pointers "wrap around" to the beginning of the array, optimizing space usage.
This avoids the need for resizing and shifting, making operations more efficient. The circular array is implemented by treating the array as a circular buffer, which ensures that all positions in the array are used optimally.
Key points:
Please Refer Implementation of Deque using circular array for implementation
| Operations | Time Complexity |
|---|---|
| insertFront(data) | O(1) |
| insertRear(data) | O(1) |
| deleteFront() | O(1) |
| deleteRear() | O(1) |
| getFront() | O(1) |
| getRear() | O(1) |
| isEmpty() | O(1) |
| size() | O(1) |
| erase() | O(n) |