- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Applications, Advantages and Disadvantages of Deque
Deque or double ended queue is a sequential linear collection data queue that provides the function like a double-ended queue. In this data structure the method does not follow the First In First Out (FIFO) rule for data treatment. This data structure is also known as double-ended queue because the elements are inserted to the end of the queue and removed from the front. For a deque we can add and remove data only from both ends. The time complexity of the deque operation is O(1). There are two types of deque −
Input Restricted
Restriction at a single end for input.
Allows data deleting from the both ends.
Output Restricted
Restriction at a single end for output.
Allows data insertion to the both ends.
Here are some commands those help a coder to perform various operation with a pool of datasets on deque −
push_back()-insert an element from the back of a deque.
push_front()-insert an element from the front of a deque.
pop_back()-removes element from the back of a deque.
pop_front()-removes element from the front of the deque.
front()-returns the element at the front of a deque.
back()-returns the element at the back of the deque.
at()-set/return at specified index.
size()- the number of elements returns.
empty()- returns true if the deque is empty.
In a circular array we can use the deque operation. If the array is full then we can start the process from the beginning. But for the linear array, if the array is full no more data can be inserted. And we can see a "overflow popup" then.
Applications of Deque
There are so many real time applications available of Deque.
For job scheduling applications.
In O(1) time we can perform clock-wise and anti-clockwise rotational operations.
The deque algorithms are also present in web browser history.
For undo operation in sorting.
Advantages of Deque
There are lots of advantages of Deque.
From the both front and back we can add and remove the data.
They are dynamic in size.
Deque provides efficient timing to perform an operation.
LIFO stack used here.
No reallocation possible here.
It is a thread safe process with proper synchronization.
Cache friendly.
Disadvantages of Deque
The disadvantages of a Deque are
Deque process have higher memory comsumption rate.
It has synchronization issues with multi thread.
Can't be implemented on all platforms.
Not suitable to implement sorting operations.
Deque has short number of functionality.
Algorithm for a deque operation
Step 1 − Consider a deque array with a size of n.
Step 2 − Set two pointers as "front=-1" for front and "rear=0" for set.
Here are so many sub parts of this process. In a deque we can perform multiple operations. We have summarized them here.
Algorithm to insert data from front in a deque:-
Step 1 − Check the front position.
Step 2 − If, "front<1" then apply "front=n-1" for last index.
Step 3 − Else, we need to decrease "front" by 1.
Step 4 − Add a new key element into the front position of an array.
Algorithm to insert data at the rear in a deque:-
Step 1 − Check the array is full or not.
Step 2 − If, it is full then apply "rear=0".
Step 3 − Else,increase the value of "rear" by 1.
Step 4 − Add a new key into "array[rear] again.
Algorithm to delete data from the front in a deque:-
Step 1 − Check the deque is empty or not.
Step 2 − If, the list is empty ("front=-1") then it is a underflow condition and deletion cannot be done.
Step 3 − If there is only one element in deque. Then "front=rear=-1".
Step 4 − Else, the "front" is at the end, then set to go "front=0".
Step 5 − Else, front=front+1.
Algorithm to delete data from the rear in a deque:-
Step 1 − Check the deque is empty or not.
Step 2 − If, it is empty ("front=-1"), deletion cannot be performed. That is an underflow condition.
Step 3 − If, the deque has only one data, then "front=rear=-1".
Step 4 − Else, follow below.
Step 5 − If, rear is at the front "rear=0". Go to front "rear = n-1".
Step 6 − Else, rear=rear-1.
Algorithm to check deque is empty or not:-
Step 1 − If front=-1, then the deque is empty.
Algorithm to check deque is full:-
Step 1 − If, front=0 and rear = n-1
Step 2 − Or, front=rear+1
Syntax of a deque
deque< object_type > deque_name; deque<int> deque1 = {11, 21, 31, 41, 51}; deque<int> deque2 {10, 20, 30, 40, 50};
In data structure, the deque inherits some properties of both stack and queues. In C++, deque is implemented as a vector of vectors.
Approach of various method with deque
Approach 1 − Implementation of a deque in a general manner
Approach 2 − Insert Elements to a Deque
Approach 3 − Access Elements from a Deque
Approach 4 − Change Elements of a Deque
Implementation of a deque in a general manner
Here in this C++ build code, we have configured a deque operation in a general manner. In this example we have inserted an element at rear end of the queue and the whole system has been executed according that manner.
Example 1
#include <iostream> using namespace std; #define MAX 10 class Deque { int arr[MAX]; int front; int rear; int size; public: Deque(int size) { front = -1; rear = 0; this->size = size; } void insertfront(int key); void insertrear(int key); void deletefront(); void deleterear(); bool isFull(); bool isEmpty(); int getFront(); int getRear(); }; bool Deque::isFull() { return ((front == 0 && rear == size - 1) || front == rear + 1); } bool Deque::isEmpty() { return (front == -1); } void Deque::insertfront(int key) { if (isFull()) { cout << "Overflow\n" << endl; return; } if (front == -1) { front = 0; rear = 0; } else if (front == 0) front = size - 1; else front = front - 1; arr[front] = key; } void Deque ::insertrear(int key) { if (isFull()) { cout << " Overflow\n " << endl; return; } if (front == -1) { front = 0; rear = 0; } else if (rear == size - 1) rear = 0; else rear = rear + 1; arr[rear] = key; } void Deque ::deletefront() { if (isEmpty()) { cout << "Queue Underflow\n" << endl; return; } if (front == rear) { front = -1; rear = -1; } else if (front == size - 1) front = 0; else front = front + 1; } void Deque::deleterear() { if (isEmpty()) { cout << " Underflow\n" << endl; return; } if (front == rear) { front = -1; rear = -1; } else if (rear == 0) rear = size - 1; else rear = rear - 1; } int Deque::getFront() { if (isEmpty()) { cout << " Underflow\n" << endl; return -1; } return arr[front]; } int Deque::getRear() { if (isEmpty() || rear < 0) { cout << " Underflow\n" << endl; return -1; } return arr[rear]; } int main() { Deque dq(4); cout << "insert element at rear end \n"; dq.insertrear(5); dq.insertrear(11); cout << "rear element: " << dq.getRear() << endl; dq.deleterear(); cout << "after deletion of the rear element, the new rear element: " << dq.getRear() << endl; cout << "insert element at front end \n"; dq.insertfront(8); cout << "front element: " << dq.getFront() << endl; dq.deletefront(); cout << "after deletion of front element new front element: " << dq.getFront() << endl; }
Output
insert element at rear end rear element: 11 after deletion of the rear element, the new rear element: 5 insert element at front end front element: 8 after deletion of front element new front element: 5
Insert Elements to a Deque
Here in this code we tried to create a C++ code to insert elements to a deque. There are two methods to perform this operation.
push_back() - insert the elements at the end of the array.
push_front() - insert the elements at the starting of an array.
Example 2
#include <iostream> #include <deque> using namespace std; int main() { deque<int> nums {16, 7}; cout << "Initial Deque As We Give: "; for (const int& num : nums) { cout << num << ", "; } nums.push_back(2001); nums.push_front(1997); cout << "\nFinal Deque Is Here: "; for (const int& num : nums) { cout << num << ", "; } return 0; }
Output
Initial Deque As We Give: 16, 7, Final Deque Is Here: 1997, 16, 7, 2001,
Access Elements from a Deque
We can access the elements from a deque by using two methods.
front() - At the front we can get a return.
back() - returns the data from back.
at() - return from a specified index.
#include <iostream> #include <deque> using namespace std; int main() { deque<int> nums {16, 07, 10}; cout << "Front element are here: " << nums.front() << endl; cout << "Back element are here: " << nums.back() << endl; cout << "Element at index 1 present here: " << nums.at(1) << endl; cout << "Element at index 0 present here: " << nums[0]; return 0; }
Output
Front element are here: 16 Back element are here: 10 Element at index 1 present here: 7 Element at index 0 present here: 16
Change Elements of a Deque
Here in this code we can use at() method to replace or change the element of that particular deque.
Example 4
#include <iostream> #include <deque> using namespace std; int main() { deque<int> nums = {07,16,10,1997,2001}; cout << "Initial Deque: "; for (const int& num : nums) { cout << num << ", "; } nums.at(0) = 2022; nums.at(1) = 10-05; cout << "\nUpdated Deque: "; for (const int& num : nums) { cout << num << ", "; } return 0; }
Output
Initial Deque: 7, 16, 10, 1997, 2001, Updated Deque: 2022, 5, 10, 1997, 2001,
Conclusion
From this article, we have learnt about the deque, its operation methods, applications, advantages and disadvantages along with algorithms and possible codes by using C++.