
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- Difference Between Priority Queue and Queue Implementation in Java?
- Should we declare it as Queue or Priority Queue while using Priority Queue in Java?
- Turn a Queue into Priority Queue
- Multithreaded Priority Queue in Python
- The Priority Queue in Javascript
- Meldable Priority Queue Operations
- Double Ended Priority Queue (DEPQ)
- How to Implement Priority Queue in Python?
- Priority Queue using Linked List in C
- Priority Queue Introduction in C/C++\n
- Double ended priority queue in C++ Program
- C++ Program to Implement Priority Queue
- Creating a Priority Queue using Javascript
- Priority Queue using doubly linked list in C++
- Priority Queue in C++ Standard Template Library (STL)
