-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
53 lines (44 loc) · 1.1 KB
/
queue.cpp
File metadata and controls
53 lines (44 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// =================== QUEUE =======================
// Demonstration of std::queue in C++
#include <iostream>
#include <queue>
using namespace std;
// Helper function to print all elements in the queue (by value)
void printQueue(queue<int> q)
{
cout << "Queue: ";
while (!q.empty())
{
cout << q.front() << " ";
q.pop();
}
cout << endl;
}
int main()
{
queue<int> q;
// Push elements
cout << "Pushing 5, 6, 7 into the queue..." << endl;
q.push(5);
q.push(6);
q.push(7);
printQueue(q);
// Size and front
cout << "Size: " << q.size() << endl;
cout << "Front: " << q.front() << endl;
cout << "Back: " << q.back() << endl;
// Pop an element
cout << "Popping front element..." << endl;
q.pop();
printQueue(q);
cout << "Front: " << q.front() << endl;
cout << "Size: " << q.size() << endl;
// Check if empty
cout << "Is queue empty? " << (q.empty() ? "Yes" : "No") << endl;
// Clear the queue
cout << "Clearing the queue..." << endl;
while (!q.empty())
q.pop();
cout << "Is queue empty after clearing? " << (q.empty() ? "Yes" : "No") << endl;
return 0;
}