Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Queue Basics in C++

C++ queue basics with FIFO order: push to the back, read front and back, pop from the front, and process a task list the same way BFS walks a graph.

BeginnerSTL Containers ProgramsExample 8 of 10
queue-basics.cpp
1#include <iostream>
2#include <queue>
3using namespace std;
4
5int main() {
6 // Create queue
7 queue<int> q;
8
9 // Enqueue elements
10 q.push(10);
11 q.push(20);
12 q.push(30);
13 q.push(40);
14 q.push(50);
15
16 cout << "Queue size: " << q.size() << endl;
17 cout << "Front element: " << q.front() << endl;
18 cout << "Back element: " << q.back() << endl;
19
20 // Display queue (by dequeuing)
21 cout << "\nQueue elements (FIFO - First In First Out):" << endl;
22 while (!q.empty()) {
23 cout << q.front() << " ";
24 q.pop();
25 }
26 cout << endl;
27
28 // Queue for BFS-like processing
29 queue<string> taskQueue;
30
31 taskQueue.push("Task 1");
32 taskQueue.push("Task 2");
33 taskQueue.push("Task 3");
34
35 cout << "\nProcessing tasks:" << endl;
36 while (!taskQueue.empty()) {
37 cout << "Processing: " << taskQueue.front() << endl;
38 taskQueue.pop();
39 }
40
41 return 0;
42}

Sample Output

Front element: 10

Oldest item.

Back element: 50

Newest item.

In this one

  • What FIFO order means
  • How front(), back(), and pop() work together
  • Why a queue keeps items in arrival order
  • How queues drive task processing and BFS

How it works

  1. 1

    Push 10 to 50; size = 5, front = 10, back = 50.

  2. 2

    Print front and pop until empty: 10 20 30 40 50.

  3. 3

    Push Task 1, Task 2, Task 3.

  4. 4

    Process each from the front.

Edge cases

front() on an empty queue

Call q.front() after the loop.

Undefined behavior. Always test empty() first.

Adding while processing

Push a new task inside the processing loop.

It joins the back and is processed after the current ones. That is exactly how BFS explores level by level.

What's going on

A queue adds at the back and removes from the front, like a line at a ticket counter. The first item in is the first out, called FIFO (first in, first out).

push adds 10 to 50 at the back. size() is 5, front() is 10 (the oldest), and back() is 50 (the newest).

Like a stack, a queue cannot be looped over directly. The display loop prints front() and then calls pop() to remove it, until the queue is empty. The output keeps the original order, 10 20 30 40 50, which is the difference from a stack.

The second part is the typical use: a line of work items. Three task names are pushed, then processed one at a time from the front. The same pattern drives breadth-first search on a graph, print spoolers, and request handling on a server. In BFS you would push new neighbours onto the back while processing from the front.

pop() returns nothing, just as with stack, so read front() first. Calling front() or pop() on an empty queue is undefined behavior, which is why the loops test empty().

All of these operations are O(1). queue is an adapter that uses a deque underneath by default.

The headers come first. <iostream> is there because it provides cout and cin for input/output operations. <queue> is there because it provides queue container class. Leave one out and the compiler stops at the first name it does not recognize.

Understanding Queue

FIFO Concept:

First In First Out
First element added is first removed
Like a line of people
Front and back accessible

Key Features:

Simple operations
O(1) time complexity
First-in-first-out order
Front and back accessible

Queue Operations

push() (Enqueue):

q.push(10); // Add to back

pop() (Dequeue):

q.pop(); // Remove from front

front():

int value = q.front(); // Access front element

back():

int value = q.back(); // Access back element

What happens here:

push(): adds to back
pop(): removes from front
front(): returns front element
back(): returns back element
All O(1) operations

Queue Properties

Size and Empty:

q.size() // Number of elements

q.empty() // True if empty

Access:

Front: first element (oldest)
Back: last element (newest)
Cannot access middle elements
FIFO order enforced

Task Processing

Using Queue:

1.Add tasks to queue
2.Process front task
3.Remove processed task
4.Repeat until empty

What happens here:

Tasks processed in order
First added = first processed
Natural for scheduling
Maintains order

When to Use Queue

Good fits:

BFS (Breadth-First Search)
Task scheduling
Print queue management
Message buffering
Order processing
Level-order traversal

Example Scenarios:

Graph BFS algorithm
Task scheduler
Print queue
Message queue
Request processing
Event handling

Important Considerations

FIFO Order:

First added = first removed
Maintains insertion order
Cannot access arbitrary elements
Natural for sequential processing

Performance:

All operations: O(1)
Very efficient
Simple implementation
Minimal overhead

Limitations:

Only front and back accessible
Cannot search or access middle
Must dequeue to access other elements
Use other containers if random access needed

return 0;

return 0; closes main and tells the operating system the program finished without an error.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Push A, B, C, then pop once. What is front()?

Pick an answer to check it.

Question 2

What does q.back() return after pushing 10, 20, 30?

Pick an answer to check it.

Questions people ask

Can I see the middle of a queue?

No. Use deque if you need index access.

What is a circular queue?

A fixed-size array where front and back wrap around. std::queue grows instead.

Which queue gives the largest item first?

priority_queue.