# Difference between Queue and Deque in C++

Queue and Deque both are linear data structures that are defined in STL of C++ programming language. Queue works on the principle of the first in first out, the element added to the queue first will be removed first, on the other hand, deque has properties to add an element either at the first index or last index, and similarly, any one of them can be removed. We will see the code of both data structures to get the exact differences.

## Basics of Queue

As we have seen above, the queue is based on the concept of the first in first out properties. We can add an element to the queue in the linear time complexity at the end and can remove or pop the element present at the beginning in the constant time.

### Syntax

Syntax of the queue in C++

queue<data_type> queue_name;


Here, data_type is the data type of the elements which we want to add to the queue such as int, long long, string, float, vector, etc. and queue_name is the name given by the programmer to the queue data structure.

Let us see some basic functions of the queue −

### Example

#include <bits/stdc++.h>
using namespace std;

// main function
int main(){
// defining a queue of integer type
queue<int>q; // q is the name given to the queue
// adding elements to the queue
q.push(1);
q.push(2);
q.push(3);
q.push(4);
// getting size of the queue
cout<<"Size of the queue is: "<<q.size()<<endl;
// getting first element of the queue
cout<<"Front or first element of the queue is: "<<q.front()<<endl;
// removing first element of queue
q.pop();
cout<<"Front element after popping one time is: "<<q.front()<<endl;
// check if queue is empty
cout<<"Is queue empty? : "<<q.empty()<<endl;
// remvoing all the elements
while(q.empty() == false){
q.pop();
}
// check if queue is empty
cout<<"Is queue empty after removing all the elements? : "<<q.empty()<<endl;
return 0;
}


### Output

Size of the queue is: 4
Front or first element of the queue is: 1
Front element after popping one time is: 2
Is queue empty? : 0
Is queue empty after removing all the elements? : 1


## Basic of Deque

Deque is a sequential data structure in which we can add or remove elements in constant time from both front and back. In vector, we can do only these operations at the rear sides, this makes the deque two-sided vector. Deque can work as a vector, stack, and queue which makes it a super set of all there.

### Syntax

Syntax of the deque in C++

deque<data_type> deque_name;


Here, data_type is the data type of the elements which we want to add to the deque such as int, long long, string, float, vector, etc. and deque_name is the name given by the programmer to the deque data structure.

### Example

Let us see some basic functions of the deque −

#include <bits/stdc++.h>
using namespace std;

// main function
int main(){
// defining a queue of integer type
deque<int>dq; // q is the name given to the queue
// adding elements to the deque from front
dq.push_front(1);
dq.push_front(2);
// getting size of the deque
cout<<"Size of the queue is: "<<dq.size()<<endl;
// adding elements to the deque from back
dq.push_back(3);
dq.push_back(4);
// print deque
for(int i=0; i<dq.size(); i++){
cout<<dq[i]<<" ";
}
cout<<endl;
// getting size of the deque
cout<<"Size of the deque is: "<<dq.size()<<endl;
// getting first element of the deque
cout<<"Front or first element of the deque is: "<<dq<<endl;
// removing first element of deque
dq.pop_front();
cout<<"Front element after popping one time is: "<<dq<<endl;
// getting last element of the deque
cout<<"Front or first element of the deque is: "<<dq.back()<<endl;
// removing first element of deque
dq.pop_back();
cout<<"Front element after popping one time is: "<<dq.back()<<endl;
// check if queue is empty
cout<<"Is deque empty? : "<<dq.empty()<<endl;
// removing all the elements
while(dq.empty() == false){
dq.pop_back();
}
// check if deque is empty
cout<<"Is deque empty after removing all the elements? : "<<dq.empty()<<endl;
return 0;
}


### Output

Size of the queue is: 2
2 1 3 4
Size of the deque is: 4
Front or first element of the deque is: 2
Front element after popping one time is: 1
Front or first element of the deque is: 4
Front element after popping one time is: 3
Is deque empty? : 0
Is deque empty after removing all the elements? : 1


## Some Key Differences between Queue and Deque

• Insertion is only rear in the queue while deque insertion can be done at both ends.

• Removal of elements can be done at both ends for deque while only from the front for the queue.

• We can access each index of the deque while for the queue only the front index is accessible.

• Deque is the supper set of the stack, vector, and queue.

• Traversal over the deque is possible using the loops without removing the elements but for the queue elements must be removed to traverse over the queue.

Basis of Comparision

Queue

Deque

Insertion

Insertion of elements can done at rear only.

Insertions can be done on both end.

Removal

Removal of elements can be done only from the front.

Removal of elements can be done at both.

Accessing elements

We can access only front element in a queue.

We can access each element in a deque.

Traversal

The queue elements must be removed to traverse over the queue.

Traversal over the deque is possible using the loops without removing the elements.

## Conclusion

In this tutorial, we have seen the difference between the queue and the deque of the C++ programming language in which we have seen their basic codes and function. Deque is a kind of supper set of the queue, stack, and vector as it can work like all three, and the queue just works on the FIFO property where the first element added to the queue will be removed first and so on.

Updated on: 24-Aug-2023

169 Views 