Queue Data Structure in C++: FIFO Concepts & Code Examples

Introduction

A queue is a linear data structure in C++ that follows the FIFO (First In, First Out) principle. This means the element that is inserted first is the first one to be removed. Queues are widely used in real-world applications such as task scheduling, printer management, buffering data, and handling requests in operating systems. In C++, queues can be implemented using arrays, linked lists, or by using the built-in queue container provided by the Standard Template Library (STL). Learning queues helps beginners understand orderly data processing and efficient resource management.

Let’s Explore the Queue Data Structure in C++

Today, we going to create Queues in C++. A queue in C++ is a linear data structure that follows FIFO (First In, First Out), where the first element added is the first one removed. It can be implemented in C++ using arrays or linked lists.

There are four types of Queues:

  1. Simple Queue – Elements are inserted at the rear and removed from the front.

  2. Circular Queue – The last position connects to the first to efficiently use space.

  3. Priority Queue – Elements are removed based on priority, not order.

  4. Double-Ended Queue (Deque) – Insertion and deletion can occur at both ends.

Queue using Array

An array-based queue stores elements in a fixed-size array using two indices, front and rear. Elements are added at the rear and removed from the front. It is fast and simple, but its size is limited and may waste space unless a circular queue is used. This is an example code of a simple queue using array:
#include <iostream> using namespace std; #define SIZE 5 class Queue { int arr[SIZE]; int front, rear; public: Queue() { front = -1; rear = -1; } void enqueue(int x) { if (rear == SIZE - 1) { cout << "Queue Overflow\n"; return; } if (front == -1) front = 0; arr[++rear] = x; } void dequeue() { if (front == -1 || front > rear) { cout << "Queue Underflow\n"; return; } cout << "Deleted: " << arr[front++] << endl; } void display() { for (int i = front; i <= rear; i++) cout << arr[i] << " "; cout << endl; } }; int main() { Queue q; q.enqueue(10); q.enqueue(20); q.enqueue(30); q.display(); q.dequeue(); q.display(); return 0; }

Now, here's another code with overall program to understand it better:

As shown in the above image, this code creates a queue using an array inside a class. The variables front and rear keep track of where elements are removed and added. When enqueue() is called, a new element is added at the rear of the array. When dequeue() is called, the element at the front is removed. If the queue is full, it shows overflow, and if it is empty, it shows underflow. The size of the queue is fixed when the object is created.

Queue using Linked List

linked list–based queue uses nodes connected by pointers. It also has front and rear pointers for removal and insertion. This type can grow dynamically and does not waste space, but it uses extra memory for pointers. This is an example code of a simple queue using linked list:
#include <iostream> using namespace std; struct Node { int data; Node* next; }; class Queue { Node *front, *rear; public: Queue() { front = rear = NULL; } void enqueue(int x) { Node* temp = new Node(); temp->data = x; temp->next = NULL; if (rear == NULL) { front = rear = temp; return; } rear->next = temp; rear = temp; } void dequeue() { if (front == NULL) { cout << "Queue Underflow\n"; return; } Node* temp = front; cout << "Deleted: " << front->data << endl; front = front->next; delete temp; if (front == NULL) rear = NULL; } void display() { Node* temp = front; while (temp != NULL) { cout << temp->data << " "; temp = temp->next; } cout << endl; } }; int main() { Queue q; q.enqueue(10); q.enqueue(20); q.enqueue(30); q.display(); q.dequeue(); q.display(); return 0; }

Now, here's the overall program of another code to understand it better:

As shown in the above image, this code implements a queue using a linked list. Each element is stored in a node containing data and a pointer to the next node. The front pointer is used to remove elements, and the rear pointer is used to add new elements. enqueue() adds a new node at the end of the list, and dequeue() removes the node from the front. This queue can grow dynamically and does not have a fixed size.

Important Notes (Things Beginners Often Miss with Queues in C++)

A very common beginner mistake is confusing FIFO with LIFO. Unlike stacks, queues always remove the element that was inserted first. Remember: insertion happens at the rear, and deletion happens at the front—never the other way around.

Another frequent issue is not handling overflow and underflow properly. In array-based queues, trying to enqueue when the queue is full causes overflow, and trying to dequeue when it’s empty causes underflow. Always check conditions like rear == SIZE - 1 or front == -1 before performing operations.

Beginners also often forget that simple array queues can waste space. After several dequeue operations, unused space appears at the front of the array. This is why circular queues are important—they reuse empty spaces efficiently instead of shifting elements.

In linked-list-based queues, a common mistake is not updating both front and rear correctly. When the last element is removed, both pointers must be set to NULL. Forgetting this can lead to dangling pointers and runtime errors.

Another overlooked detail is accessing queue elements without checking if the queue is empty. Calling dequeue or reading front data when front == NULL can crash the program. Defensive checks make your code safer and more professional.

Lastly, beginners sometimes underestimate queues’ importance. Queues are heavily used in real systems, including CPU scheduling, networking, printer queues, and breadth-first search (BFS). Understanding queues early makes it much easier to learn operating systems, graphs, and real-time applications later on.

Mastering queues—along with stacks—gives you a strong foundation in data flow and algorithm design in C++.

Conclusion

Queues are an essential data structure for managing data in a sequential and organized manner. Their FIFO behavior makes them ideal for applications where fairness and order matter. By understanding queues in C++, beginners develop strong problem-solving skills and gain insight into how real-time systems handle tasks. Mastering queues also builds a solid foundation for advanced concepts such as circular queues, priority queues, and breadth-first search algorithms.

Comments

Popular posts from this blog

Numbers & Numeric Operations in C++: Data Types & cmath Functions

Introduction to C++: Your First Program & Hello World

Intro to C++ for Beginners

User Input in C++: Reading Data from Keyboard with cin & getline()

Mad Libs Game in C++: Build Your First Interactive Program

Strings in C++: Basics, Methods & Examples

Variables & Data Types in C++: Basics with Examples

Printing Patterns in C++: Shape Output with Loops & Logic

Return Statement in C++: Syntax, Purpose & Examples

Functions in C++: Syntax, Use & Examples