The Wayback Machine - https://web.archive.org/web/20240929162840/https://www.geeksforgeeks.org/difference-between-queue-and-deque-in-c/
Open In App

Difference between Queue and Deque in C++

Last Updated : 20 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Queue: A Queue is a linear data structure that follows a First In First Out (FIFO) order in which the operations are performed. It is a type of container adaptor where elements are inserted into one end of the container and deleted from the other.

Functions:

  • empty(): Tests whether the queue is empty.
  • size(): Returns the unsigned int, size of the queue.
  • queue::front() and queue::back(): front() function returns a reference to the first element or the oldest of the queue. back() function returns a reference to the last or the newest element of the queue.
  • push(k) and pop(): push() function adds the element ‘k’ at the end of the queue. pop() function deletes the element from the beginning of the queue and reduces its size by 1.
  • swap(): exchanges the elements of two different queues of the same type but may or may not of the same size.
  • emplace(): it is used to insert a new element at end of the queue.

Syntax:

queue <data_type> q

Below is the program to illustrate the same:

C++




// C++ program to demonstrate the
// working of queue
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
    // Declare a queue
    queue<int> q;
 
    // Insert elements in the queue
    q.push(10);
    q.push(5);
    q.push(15);
    q.push(1);
 
    // Delete elements from the queue
    q.pop();
    q.pop();
 
    cout << "Elements in Queue are: ";
 
    // Print the element stored
    // in queue
    while (!q.empty()) {
        cout << q.front() << ' ';
 
        // Pop the front element
        q.pop();
    }
 
    return 0;
}


Output: 

Elements in Queue are: 15 1

 

Deque: Deque is a sequence container with the ability of expansion and contraction on both ends. It is a template of Standard Template Library or STL in C++is. It is similar to vectors but are more efficient for the insertion and deletion of elements. Contiguous storage allocation in deque may not be guaranteed as in vectors.

Functions:

  • max_size(): Returns the maximum number of elements deque can contain.
  • push_back() and push_front(): push_front( ) push the elements into a deque from the front and push_back( ) push elements into a deque from the back.
  • pop_front() and pop_back(): pop_front() function is used to pop elements from a deque from the front and pop_back( ) function is used to pop elements from a deque from the back.
  • clear() and erase():  clear is used to remove all the elements from the deque and erase is used to remove some specified elements.
  • insert(): increases the container side by inserting element in the specified position.
  • resize(): changes the size of the element’s container as per requirement.
  • rbegin() and rend(): rbegin() points to the last element of the deque whereas rend points to the position before the beginning of the deque.
  • at() and swap(): at() points to the position of the element given in the parameter and swap( ) is used two swap elements of two deques.
  • emplace_front() and emplace_back(): these two functions are used to insert new elements in the container at the beginning and at the end of deque respectively.

Syntax:

deque<data_type> dq

Below is the program to illustrate the same:

C++




// C++ program to demonstrate the
// working of deque
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
    // Declare a deque
    deque<int> dq;
 
    // Insert element in the front
    dq.push_front(10);
    dq.push_front(5);
    dq.push_front(3);
 
    // Delete elements from the front
    dq.pop_front();
    dq.pop_front();
 
    // Insert elements in the back
    dq.push_back(1);
    dq.push_back(50);
    dq.push_back(2);
 
    // Delete elements from the back
    dq.pop_back();
    dq.pop_back();
 
    cout << "Elements in deque are: ";
 
    // Print the element stored
    // in deque
    while (!dq.empty()) {
        cout << " " << dq.front();
        dq.pop_front();
    }
 
    return 0;
}


Output: 

Elements in deque are:  10 1

 

Below is the tabular difference between the queue and deque:

S.No.

Queue

Deque

1 Insertion can be done through the rear end only. Insertion is possible through both ends.
2 Deletion of elements is possible through the front end only. Deletion of elements possible through both ends.
3 Elements can not be accessed through iterators. Elements can be accessed through iterators.
4 Implemented as container adaptors. Implemented generally as some form of a dynamic array.
5 The stack can not be implemented using a queue. A stack can be implemented using deque.


Previous Article
Next Article

Similar Reads

Difference between Queue and Deque (Queue vs. Deque)
Queue: The queue is an abstract data type or linear data structure from which elements can be inserted at the rear(back) of the queue and elements can be deleted from the front(head) of the queue. The operations allowed in the queue are:insert an element at the reardelete element from the frontget the last elementget the first elementcheck the size
3 min read
Difference between queue.queue vs collections.deque in Python
Both queue.queue and collections.deque commands give an idea about queues in general to the reader but, both have a very different application hence shouldn't be confused as one. Although they are different and used for very different purposes they are in a way linked to each other in terms of complete functionality. Before we jump into what they a
3 min read
Difference Between deque::cbegin and deque::assign in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::at in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
3 min read
Difference Between deque::assign and deque::back in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Deque::assign deque::assign is
3 min read
Difference Between deque::begin and deque::assign in C++
Deque or Double-ended queue are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the difference
3 min read
Difference Between deque::assign and deque::empty in C++
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the differenc
2 min read
deque::at() and deque::swap() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::at()at() function is us
4 min read
deque::pop_front() and deque::pop_back() in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::pop_front() pop_front()
4 min read
deque::clear() and deque::erase() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::clear() The clear()
5 min read
deque::operator= and deque::operator[] in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::operator= This operator
4 min read
deque::begin() and deque::end in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::begin()begin() function
3 min read
deque::emplace_front() and deque::emplace_back() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::emplace_front()This fun
5 min read
Deque::front() and deque::back() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque. deque::front()fron
4 min read
Deque::empty() and deque::size() in C++ STL
Deque or Double Ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed in the deque. deque::empty() emp
4 min read
Should we declare as Queue or Priority Queue while using Priority Queue in Java?
Queue: Queue is an Interface that extends the collection Interface in Java and this interface belongs to java.util package. A queue is a type of data structure that follows the FIFO (first-in-first-out ) order. The queue contains ordered elements where insertion and deletion of elements are done at different ends. Priority Queue and Linked List are
3 min read
Difference Between Linear Queue and Circular Queue
Queues are fundamental data structures in computer science that follow the First-In-First-Out (FIFO) principle. Among the various types of queues, linear queues and circular queues are commonly used. While they share some similarities, they differ significantly in structure and operational efficiency. This article explores the concepts, advantages,
4 min read
Difference between Circular Queue and Priority Queue
Queues are fundamental data structures that are used to store and manage a collection of elements. While both circular queues and priority queues are types of queues, they have distinct characteristics and applications. This article will explore the key differences between circular queues and priority queues. Circular Queue:A Circular Queue is an e
4 min read
Difference between Multilevel Queue (MLQ) and Multi Level Feedback Queue (MLFQ) CPU Scheduling Algorithms
Multilevel Queue Scheduling partitions the ready queue into several separate queues. And a Multilevel Feedback Queue Scheduling allows a process to move between queues. In this article, we will learn the differences between Multilevel Queue (MLQ) and Multi-Level Feedback Queue (MLFQ) CPU scheduling algorithms. Multilevel Queue (MLQ) Scheduling Algo
5 min read
Implement Stack and Queue using Deque
Deque also known as double ended queue, as name suggests is a special kind of queue in which insertions and deletions can be done at the last as well as at the beginning. A link-list representation of deque is such that each node points to the next node as well as the previous node. So that insertion and deletions take constant time at both the beg
15 min read
queue::empty() and queue::size() in C++ STL
Queue is a type of container adaptor that operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::empty() empty() function is used to check if the queue container is empty or not. SyntaxqueueName.empty()ParametersThis function does not accept any parameter.Return Val
4 min read
queue::front() and queue::back() in C++ STL
Queue are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front. queue::front() This function is used to reference the first or the oldest element of the queue container. This function can be used to fetch the first element of a queue.Syn
3 min read
Stack and Queue in Python using queue Module
A simple python List can act as queue and stack as well. Queue mechanism is used widely and for many purposes in daily life. A queue follows FIFO rule(First In First Out) and is used in programming for sorting and for many more things. Python provides Class queue as a module which has to be generally created in languages such as C/C++ and Java. 1.
3 min read
Check if a queue can be sorted into another queue using a stack
Given a Queue consisting of first n natural numbers (in random order). The task is to check whether the given Queue elements can be arranged in increasing order in another Queue using a stack. The operation allowed are: Push and pop elements from the stack Pop (Or Dequeue) from the given Queue. Push (Or Enqueue) in the another Queue. Examples : Inp
9 min read
Reversing a Queue using another Queue
Given a queue. The task is to reverse the queue using another empty queue. Examples: Input: queue[] = {1, 2, 3, 4, 5} Output: 5 4 3 2 1 Input: queue[] = {10, 20, 30, 40} Output: 40 30 20 10 Approach: Given a queue and an empty queue.The last element of the queue should be the first element of the new queue.To get the last element there is a need to
5 min read
Advantages of circular queue over linear queue
Linear Queue: A Linear Queue is generally referred to as Queue. It is a linear data structure that follows the FIFO (First In First Out) order. A real-life example of a queue is any queue of customers waiting to buy a product from a shop where the customer that came first is served first. In Queue all deletions (dequeue) are made at the front and a
3 min read
Why can't a Priority Queue wrap around like an ordinary Queue?
Priority Queue: A priority queue is a special type of queue in which each element is assigned a priority value. And elements are served based on their priority. This means that elements with higher priority are served first. However, if elements with the same priority occur, they will be served in the order in which they were queued. A priority que
3 min read
Can we use Simple Queue instead of Priority queue to implement Dijkstra's Algorithm?
What is Dijkstra's Algorithm? Dijkstra's Algorithm is used for finding the shortest path between any two vertices of a graph. It uses a priority queue for finding the shortest path. For more detail, about Dijkstra's Algorithm, you can refer to this article. Why Dijkstra's Algorithm uses a Priority Queue? We use min heap in Dijkstra's Algorithm beca
2 min read
Turn a Queue into a Priority Queue
What is Queue?Queue is an abstract data type that is open at both ends. One end is always used to insert data (enqueue) which is basically the rear/back/tail end and the other which is the front end is used to remove data (dequeue). Queue follows First-In-First-Out (FIFO) methodology, i.e., "the data item stored first will be accessed first". Decla
9 min read
Why does Queue have front but Priority-queue has top in stl?
Why does the queue have front but the priority queue has top in stl? The main difference between a queue and a priority queue is that a queue follows the FIFO (First-In-First-Out) principle, while a priority queue follows a specific priority order. In other words, the elements in a queue are processed in the order they were added, whereas the eleme
8 min read
Article Tags :
Practice Tags :