A queue is a linear data structure that operates on the principle of First In, First Out (FIFO). It is widely used in various computing problems where tasks or resources are processed in the same order they are added. The queue can be implemented in different ways, and one of the most efficient ways is through an array.
This article explores the concept of a queue using an array, its implementation in multiple programming languages such as C, C++, and Java, and its advantages, disadvantages, and common applications.
Article Metadata:
Queues are linear data structures that follow the First In First Out (FIFO) principle. As a result, the element that is inserted first will be removed first. A queue has two primary operations:
The queue is typically represented by two variables:
In an array-based implementation:
The basic operations of a queue using an array are:
The enqueue operation performs the following:
Algorithm for Enqueue Operation:
1. Check if the queue is full (rear == size of the array).
2. If the queue is full, print "Queue is full" and exit.
3. If the queue is not full:
a. Increment the rear index.
b. Insert the new element at the rear index in the array.
The dequeue operation performs the following:
Algorithm for Dequeue Operation:
1. Check if the queue is empty (front == rear).
2. If the queue is empty, print "Queue is empty" and exit.
3. If the queue is not empty:
a. Get the value at the front index of the array.
b. Increment the front index.
Let's use an array of size 5 to demonstrate the queue operations with an example.
We start with two variables, front and rear both initialized to -1. This indicates that the queue is empty initially.
Before Operation: front = -1, rear = -1
We insert the value 10 at the rear of the queue. Since the queue is empty, both front and rear will point to index 0.
After Operation:
Before Operation: front = 0, rear = 0
We insert the value 20 at the rear of the queue. The rear is incremented to 1, and the value 20 is added at position 1.
After Operation:
Before Operation: front = 0, rear = 1
We remove the value at the front of the queue, which is 10 (the value at index 0). After removing this element, the front is incremented to 1.
After Operation:
Before Operation: front = 1, rear = 1
We insert the value 30 at the rear of the queue. The rear is incremented to 2, and the value 30 is added at position 2.
After Operation:
Before Operation: front = 1, rear = 2
We insert the value 40 at the rear of the queue. The rear is incremented to 3, and the value 40 is added at position 3.
After Operation:
Before Operation: front = 1, rear = 3
We remove the value at the front of the queue, which is 20 (the value at index 1). After removing this element, the front is incremented to 2.
After Operation:
The following table summarizes the state of the queue after each operation:
| Operation | Front | Rear | Queue Array |
|---|---|---|---|
| Initial | -1 | -1 | [] |
| Enqueue(10) | 0 | 0 | [10] |
| Enqueue(20) | 0 | 1 | [10, 20] |
| Dequeue() | 1 | 1 | [20] |
| Enqueue(30) | 1 | 2 | [20, 30] |
| Enqueue(40) | 1 | 3 | [20, 30, 40] |
| Dequeue() | 2 | 3 | [30, 40] |
Using an array to implement a queue has several advantages. Arrays are contiguous blocks of memory, which allows for easy access to elements via an index. This makes it easy to manage the front and rear of the queue efficiently. The main reasons to use an array for a queue implementation are:
Here is the implementation of the queue using array in C, C++, and Java languages:
Below is a simple C program to implement a queue using an array.
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
struct Queue {
int items[MAX];
int front, rear;
};
void initQueue(struct Queue* q) {
q->front = -1;
q->rear = -1;
}
int isFull(struct Queue* q) {
return q->rear == MAX - 1;
}
int isEmpty(struct Queue* q) {
return q->front == -1;
}
void enqueue(struct Queue* q, int value) {
if (isFull(q)) {
printf("Queue is full\n");
return;
}
if (q->front == -1) {
q->front = 0;
}
q->rear++;
q->items[q->rear] = value;
printf("Enqueued: %d\n", value);
}
int dequeue(struct Queue* q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return -1;
}
int item = q->items[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else {
q->front++;
}
return item;
}
int main() {
struct Queue q;
initQueue(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
printf("Dequeued: %d\n", dequeue(&q));
return 0;
}
#include <iostream>
using namespace std;
#define MAX 5
class Queue {
private:
int arr[MAX];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
bool isFull() {
return rear == MAX - 1;
}
bool isEmpty() {
return front == -1;
}
void enqueue(int value) {
if (isFull()) {
cout << "Queue is full!" << endl;
return;
}
if (front == -1) front = 0;
rear++;
arr[rear] = value;
cout << "Enqueued: " << value << endl;
}
int dequeue() {
if (isEmpty()) {
cout << "Queue is empty!" << endl;
return -1;
}
int dequeuedValue = arr[front];
if (front == rear) {
front = rear = -1;
} else {
front++;
}
return dequeuedValue;
}
};
int main() {
Queue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
cout << "Dequeued: " << q.dequeue() << endl;
return 0;
}
public class Queue {
private int[] arr;
private int front, rear;
private int maxSize;
public Queue(int size) {
arr = new int[size];
front = -1;
rear = -1;
maxSize = size;
}
public boolean isFull() {
return rear == maxSize - 1;
}
public boolean isEmpty() {
return front == -1;
}
public void enqueue(int value) {
if (isFull()) {
System.out.println("Queue is full!");
return;
}
if (front == -1) front = 0;
rear++;
arr[rear] = value;
System.out.println("Enqueued: " + value);
}
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty!");
return -1;
}
int dequeuedValue = arr[front];
if (front == rear) {
front = rear = -1;
} else {
front++;
}
return dequeuedValue;
}
public static void main(String[] args) {
Queue q = new Queue(5);
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
System.out.println("Dequeued: " + q.dequeue());
}
}
Here are the key applications of queue using an array:
Here are some of the advantages of queue using array:
Here are the disadvantages of queue using an array:
In conclusion, implementing a queue using an array is a simple and powerful method for managing data in a FIFO order. It is suitable for use cases where the maximum size of the queue is known and remains constant. However, for dynamic scenarios, more advanced structures like linked lists or dynamic arrays may be better suited.
Yes, the queue size is fixed when using an array because the array is statically allocated with a predefined maximum size.
Yes, a priority queue can be implemented using an array in programming languages like Java and C++, where elements are inserted in such a way that the highest (or lowest) priority element is always at the front.
The following related articles are available on the NxtWave CCBP Blog:
Mastering Insertion Sort in C: Code, Logic, and Applications - Understand insertion sort in C with easy-to-follow logic, code examples, and practical tips. Learn how this sorting technique works in real programs. (29 Dec 2025, 6 min read)
Quick Sort Algorithm Explained: Steps, Code Examples and Use Cases - Learn the Quick Sort Algorithm with clear steps, partition logic, Python & C++ code examples, and time complexity explained for students and developers. (28 Dec 2025, 6 min read)
Linked List in Data Structure: A Complete Guide - Learn what a Linked List in Data Structure is, its types, operations, advantages, and real-world uses. A complete guide for students and developers. (27 Dec 2025, 8 min read)
Contiguous Memory Allocation: A Complete Guide - Learn about contiguous memory allocation in OS, its advantages, disadvantages, and implementation methods. Explore how it optimizes memory management. (26 Dec 2025, 7 min read)
A Complete Guide on Stack Using Array in C - Learn how to implement a stack using an array in C. Explore basic stack operations like push, pop, and peek with code examples and explanations for beginners. (26 Dec 2025, 6 min read)
Understand the Concept of Backtracking in DAA with Examples - Explore backtracking in DAA, its features, applications, efficiency improvements, and key concepts to understand this problem-solving technique. (26 Dec 2025, 12 min read)
This article is published by NxtWave, an educational technology organization providing industry-recognized certifications and training programs. For more information, visit www.ccbp.in.
Contact Information:
Course Offerings: