Queue Basics

Basic Queue Operations in C++

BeginnerTopic: STL Containers Programs
Back

C++ Queue Basics Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
#include <queue>
using namespace std;

int main() {
    // Create queue
    queue<int> q;
    
    // Enqueue elements
    q.push(10);
    q.push(20);
    q.push(30);
    q.push(40);
    q.push(50);
    
    cout << "Queue size: " << q.size() << endl;
    cout << "Front element: " << q.front() << endl;
    cout << "Back element: " << q.back() << endl;
    
    // Display queue (by dequeuing)
    cout << "\nQueue elements (FIFO - First In First Out):" << endl;
    while (!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;
    
    // Queue for BFS-like processing
    queue<string> taskQueue;
    
    taskQueue.push("Task 1");
    taskQueue.push("Task 2");
    taskQueue.push("Task 3");
    
    cout << "\nProcessing tasks:" << endl;
    while (!taskQueue.empty()) {
        cout << "Processing: " << taskQueue.front() << endl;
        taskQueue.pop();
    }
    
    return 0;
}
Output
Queue size: 5
Front element: 10
Back element: 50

Queue elements (FIFO - First In First Out):
10 20 30 40 50

Processing tasks:
Processing: Task 1
Processing: Task 2
Processing: Task 3

Understanding Queue Basics

This program teaches you how to use Queue Basics in C++. Queue is a FIFO (First In First Out) container where elements are added at the back and removed from the front. It's perfect for processing tasks in order, BFS algorithms, and maintaining first-come-first-served order.

---

1. What This Program Does

The program demonstrates queue operations:

Creating queue and enqueuing elements
Accessing front and back elements
Dequeuing elements (FIFO order)
Task processing simulation

Queues provide simple, efficient first-in-first-out data access.

---

2. Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <queue>
Provides queue container class.

---

3. 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

---

4. 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

How it works

:

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

---

5. 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

---

6. Task Processing

Using Queue

:

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

How it works

:

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

---

7. When to Use Queue

Best For

:

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

---

8. 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

---

9. return 0;

This ends the program successfully.

---

Summary

Queue: FIFO (First In First Out) container, elements added at back, removed from front.
Operations: push(), pop(), front(), back(), empty(), size() - all O(1).
Common uses: BFS, task scheduling, print queue, message buffering.
Understanding queues enables sequential processing and order maintenance.
Essential for algorithms requiring FIFO behavior.

This program is fundamental for learning queue data structure, understanding FIFO operations, and preparing for BFS and task scheduling algorithms in C++ programs.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Queue Basics

This C++ program is part of the "STL Containers Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents