std::queue<int> q; q.push(10); q.push(20); q.push(30); q.pop(); q.push(40); // Print queue elements from front to back std::queue<int> temp = q; while (!temp.empty()) { std::cout << temp.front() << " -> "; temp.pop(); } std::cout << "null";
Remember that pop() removes the front element in a queue.
The queue starts with 10, 20, 30. After pop(), 10 is removed. Then 40 is added at the back. So the queue is 20, 30, 40.
Think about a line of people waiting for service.
FIFO means First In, First Out. The first element added to the queue is the first one to be removed.
std::queue<int> q; q.push(1); q.pop(); q.pop(); // Extra pop
Check what happens if you remove more elements than present.
Calling pop() on an empty queue causes undefined behavior or runtime error.
Think about who gets served first in a line.
People waiting in line are served in the order they arrive, matching the FIFO queue behavior.
std::queue<int> q; q.push(5); q.push(10); q.push(15); q.pop(); q.push(20); q.pop(); q.push(25); std::queue<int> temp = q; while (!temp.empty()) { std::cout << temp.front() << " -> "; temp.pop(); } std::cout << "null";
Track each push and pop carefully in order.
After first pop, 5 is removed. Queue: 10,15. Then push 20: 10,15,20. Next pop removes 10: 15,20. Then push 25: 15,20,25.
