To understand the implementation of a queue using a deque using C++
Objective
To understand the implementation of a queue using a deque using C++
#include <iostream>
#include <deque>
using namespace std;
int main() {
// Declare a deque
deque<int> dq;
// Insert elements at the rear
for(int i = 1; i <= 5; i++) {
dq.push_back(i);
}
// Print the queue
cout << "The queue is: ";
for(auto i = dq.begin(); i != dq.end(); i++) {
cout << *i << " ";
}
cout << endl;
// Pop element from the front
dq.pop_front();
// Print the queue after popping
cout << "After popping front, the queue is: ";
for(auto i = dq.begin(); i != dq.end(); i++) {
cout << *i << " ";
}
cout << endl;
return 0;
}
Algorithm
- Start
- Declare and initialize a double ended queue
- Add elements at the rear end
- Remove elements from the front
- End
Representations
Flow Diagram
+----------------------------------+
| |
| Start |
| | |
+----------------------------------+
|
V
+----------------------------------+
| |
| Declare deque dq |
| | |
+----------------------------------+
|
V
+----------------------------------+
| |
| Insert elements at the rear |
| | |
+----------------------------------+
|
V
+----------------------------------+
| |
| Print the queue |
| | |
+----------------------------------+
|
V
+----------------------------------+
| |
| Pop element from the front |
| | |
+----------------------------------+
|
V
+----------------------------------+
| |
| Print the queue after popping |
| | |
+----------------------------------+
|
V
+----------------------------------+
| |
| Exit |
| | |
+----------------------------------+
Tabular Dry Run
push | pop
1 |
2 |
3 |
4 |
5 |
| 1
Results/Output
The queue is: 1 2 3 4 5 After popping front, the queue is: 2 3 4 5