Queue
Last updated
Was this helpful?
Was this helpful?
#include <queue>queue<type> q;// Create a queue of integer data type
queue<int> Integer_Queue;
// Create a queue of float data type
queue<float> float_Queue;
// Create a queue of string data type
queue<string> String_Queue;#include <iostream>
#include <queue>
using namespace std;
// Print the queue element
void Print_Queue (queue<int> Queue){
queue<int> Q = Queue;
while (!Q.empty()) {
cout << '\t' << Q.front();
Q.pop();
}
}
int main() {
std::queue<int> q;
q.push(10); // Insert an element at the back of the queue
q.push(20);
q.push(30);
q.push(40);
q.push(50);
q.push(60);
cout << "Size of queue: " << q.size() << endl;
cout << "Front element: " << q.front() << endl;
cout << "Back element: " << q.back() << endl;
q.pop(); // Remove front an element from the front of the queue
cout << "Size of queue after pop: " << q.size() << endl;
cout << "Front element after pop: " << q.front() << endl;
cout << "Back element after pop: " << q.back() << endl;
cout << "Elements in the queue: ";
Print_Queue(q);
cout << endl;
return 0;
}Size of queue: 6
Front element: 10
Back element: 60
Size of queue after pop: 5
Front element after pop: 20
Back element after pop: 60
Elements in the queue: 20 30 40 50 60#include <iostream>
#include <queue>
using namespace std;
// Print the queue element
void Print_Queue (queue<int> Queue){
queue<int> Q = Queue;
while (!Q.empty()) {
cout << ' ' << Q.front();
Q.pop();
}
}
int main() {
std::queue<int> q; // declare Queue
std::queue<int> T; // declare Queue
for (int i = 1; i <= 20; i++){
q.push(i);
}
cout << "Returns the first element of the queue: " << q.front() << endl;
cout << "Returns the last element of the queue: " << q.back() << endl;
cout << "Returns the number of elements in the queue: " << q.size() << endl;
cout << "Returns true if the queue of q is empty: " << q.empty() << endl;
cout << "Returns true if the queue of T is empty: " << T.empty() << endl;
// 1(true) - if the queue is empty. 0(false) -if the queue is not empty.
cout << "Elements in the queue: ";
Print_Queue(q);
cout << endl;
return 0;
}++Returns the first element of the queue: 1
Returns the last element of the queue: 20
Returns the number of elements in the queue: 20
Returns true if the queue of q is empty: 0
Returns true if the queue of T is empty: 1
Elements in the queue: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20