
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Dequeue and Priority Queue in C++
As we know that the queue data structure is First in First Out data structure. The queue has some variations also. These are the Dequeue and the Priority Queue.
The Dequeue is basically double ended queue. So there are two front and two rear pairs. One pair of front and rear pointer is used to describe the queue from left side, and another one is used to describe it from the right side. We can insert or delete elements from both side in this structure. Here we will see some C++ code using dequeue STL to understand its functionality.
Example (Dequeue)
#include <iostream> #include <deque> using namespace std; void dequeElements(deque <int> que) { deque <int> :: iterator it; for (it = que.begin(); it != que.end(); ++it) cout << *it << " "; cout <<endl; } int main() { deque <int> que; que.push_back(10); que.push_front(20); que.push_back(30); que.push_front(15); cout << "Currently que is holding : "; dequeElements(que); cout <<"Size of dequeue : " <<que.size() << endl; cout << "Element at position 2 : " << que.at(2) << endl; cout << "Element at front position : " << que.front() << endl; cout << "Element at back position : " << que.back() << endl; cout << "Delete from front side : "; que.pop_front(); dequeElements(que); cout << "Delete from back side : "; que.pop_back(); dequeElements(que); }
Output
Currently que is holding : 15 20 10 30 Size of dequeue : 4 Element at position 2 : 10 Element at front position : 15 Element at back position : 30 Delete from front side : 20 10 30 Delete from back side : 20 10
Another variation of queue is the priority queue. In this structure, each element in the queue has its own priority. When we insert item into queue, we have to assign priority value with it. It will delete the highest priority element at first. To implement priority queue one of the easiest method is using the heap data structure.
Let us see one C++ code for priority queue STL. Here the priority is assigned based on the value. So higher value will be treated as highest priority element.
Example (Priority Queue)
#include <iostream> #include <queue> using namespace std; void dequeElements(priority_queue <int> que) { priority_queue <int> q = que; while(!q.empty()){ cout << q.top() << " "; q.pop(); } cout << endl; } int main() { priority_queue <int> que; que.push(10); que.push(20); que.push(30); que.push(5); que.push(1); cout << "Currently que is holding : "; dequeElements(que); cout << "Size of queue : " <<que.size() << endl; cout << "Element at top position : " << que.top() << endl; cout << "Delete from queue : "; que.pop(); dequeElements(que); cout << "Delete from queue : "; que.pop(); dequeElements(que); }
Output
Currently que is holding : 30 20 10 5 1 Size of queue : 5 Element at top position : 30 Delete from queue : 20 10 5 1 Delete from queue : 10 5 1