Queue STL Implementation
#include <bits/stdc++.h> // Includes <queue> and other standard librariesusing namespace std; // Uses the standard namespace
int main() {
queue<int> q; // Declare a queue of integers
q.push(11); // Add 11 to the back of the queue // Queue: [11] (front=11, back=11)
cout << "Front element of q : " << q.front() << endl; // Access the front element (11) cout << "Back element of q : " << q.back() << endl; // Access the back element (11)
q.push(15); // Add 15 to the back // Queue: [11, 15] (front=11, back=15) cout << "Front element of q : " << q.front() << endl; // Front element is still 11 cout << "Back element of q : " << q.back() << endl; // Back element is now 15
q.push(23); // Add 23 to the back q.push(30); // Add 30 to the back // Queue: [11, 15, 23, 30]
cout << "Size of queue : " << q.size() << endl; // Get the number of elements (4)
cout << endl << "Front before pop : " << q.front() << endl; // Front element: 11 cout << "Back before pop : " << q.back() << endl << endl; // Back element: 30
q.pop(); // Remove the front element (11) // Queue: [15, 23, 30]
cout << "Front after pop : " << q.front() << endl; // New front element: 15 cout << "Back after pop : " << q.back() << endl << endl; // Back element remains 30
q.pop(); // Remove 15 q.pop(); // Remove 23 // Queue: [30]
cout << "Size of queue : " << q.size() << endl; // Size is now 1
q.pop(); // Remove 30 // Queue: [] (empty)
// Check if the queue is empty if(q.empty()) { cout << "Queue is empty!" << endl; } else { cout << "Queue is not empty!" << endl; }
return 0; // End of program}
Concept
- Queue: A First-In, First-Out (FIFO) data structure.
- Elements are added to the
back
(orrear
) and removed from thefront
. - Analogy: A line of people where the first person in line is the first to be served.
Key Operations
push(element)
: Adds anelement
to theback
of the queue.pop()
: Removes the element from thefront
of the queue. Does not return the element. You must usefront()
first to retrieve the value.front()
: Returns a reference to the element at thefront
of the queue. Does not remove it.back()
: Returns a reference to the element at theback
of the queue. Does not remove it.size()
: Returns the number of elements currently in the queue.empty()
: Returnstrue
if the queue is empty,false
otherwise.
Important Notes
- Always check
!q.empty()
before callingq.front()
orq.pop()
to avoid undefined behavior (accessing an empty queue). std::queue
is a container adapter. By default, it usesstd::deque
as its underlying container. You can specify other sequence containers (likestd::list
) but notstd::vector
.