fork download
  1. #include <iostream>
  2. #include <queue> // Required for using the queue container
  3. using namespace std;
  4.  
  5. int main() {
  6. // 1. Initialize an empty queue of integers
  7. queue<int> q1; //
  8.  
  9. // push(x): Adds elements to the back of the queue
  10. q1.push(10); // 10 is the FIRST element in
  11. q1.push(20); //
  12. q1.push(30); // 30 is the LAST element in
  13.  
  14. // size(): Returns the current number of elements
  15. cout << "Queue size after PUSH: " << q1.size() << endl; // Output: 3
  16.  
  17. // front(): Accesses the element at the front (10)
  18. cout << "Front element: " << q1.front() << endl; // Output: 10
  19.  
  20. // back(): Accesses the element at the back (30)
  21. cout << "Back element: " << q1.back() << endl; // Output: 30
  22.  
  23. // pop() removes the front element
  24. q1.pop(); // The queue after pop: 20, 30
  25.  
  26. // Final empty() check
  27. cout << "Empty? " << (q1.empty() ? "Yes" : "No") << endl; // Output: No
  28. return 0;
  29. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Queue size after PUSH: 3
Front element: 10
Back element: 30
Empty? No