Queue Implementation Using Array
By Sriram
Updated on Jul 22, 2026 | 4.2K+ views
Share:
All courses
Certifications
More
By Sriram
Updated on Jul 22, 2026 | 4.2K+ views
Share:
Table of Contents
Key Highlights
Take your programming and data skills to the next level with upGrad's Data Science programs. Learn Python, data structures, machine learning, and AI through hands-on projects, expert mentorship, and career support.
Popular Data Science Programs
A queue stores elements in the order they arrive. The first element inserted into the queue is also the first one removed. That's why queues follow the FIFO (First In, First Out) rule.
In queue implementation using array, all queue elements are stored inside a fixed-size array. Two variables keep track of the queue.
The front points to the first element available for deletion.
The rear points to the most recently inserted element.
This style of implementation of queue using array is widely used when the maximum number of elements is known in advance. Since arrays provide direct memory access, insertion and deletion checks are straightforward and easy to implement.
Imagine a queue with a capacity of five elements.
Index |
0 |
1 |
2 |
3 |
4 |
| Value | 12 | 18 | 25 | Empty | Empty |
Here,
If another value is inserted, the rear pointer moves one position to the right.
If the first value is removed, the front pointer also moves forward.
This is the basic idea behind queue implementation using array in data structure.
Also read : queue implementation.
The implementation of queue using array follows a straightforward approach in which a fixed-size array stores elements, while two pointers (front and rear) keep track of the first and last elements in the queue. Every insertion takes place at the rear, and every deletion occurs from the front, ensuring the queue follows the First In, First Out (FIFO) principle.
To build the implementation of queue using array, you first declare an array, initialize the pointers, and then perform queue operations such as enqueue, dequeue, peek, and status checks for overflow and underflow. Although programming syntax varies across C, C++, Java, and Python, the underlying logic remains the same.
Create an array with a fixed capacity to store queue elements. The array serves as the queue's storage, where each element occupies a specific index. The maximum size is fixed during declaration, allowing efficient access while limiting the total number of elements the queue can hold.
queue[MAX]
Before performing any queue operation, initialize both the front and rear pointers to -1 to indicate that the queue is empty. These pointers track the first and last elements in the queue and are updated whenever elements are inserted or removed.
front = -1
rear = -1
Before inserting a new element:
Before removing an element:
Before every insertion or deletion:
These validation checks ensure that queue operations are performed safely and efficiently.
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = -1, rear = -1;
void enqueue(int value) {
if (rear == SIZE - 1) {
printf("Queue Overflow\n");
return;
}
if (front == -1)
front = 0;
queue[++rear] = value;
}
void dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return;
}
printf("Deleted: %d\n", queue[front]);
front++;
if (front > rear)
front = rear = -1;
}
void display() {
int i;
if (front == -1) {
printf("Queue is Empty\n");
return;
}
printf("Queue: ");
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
}
Output
Queue: 10 20 30
Deleted: 10
Queue: 20 30
Also read : guide on dynamic memory n C.
Data Science Courses to upskill
Explore Data Science Courses for Career Progression
Once a queue has been created, it becomes useful only when you can perform operations on it. Every queue implementation using array supports a few core operations that allow you to insert, remove, and inspect elements while maintaining the FIFO principle.
Each operation has a specific purpose. Together, they make the implementation of queue using array suitable for applications such as task scheduling, printer queues, and buffering systems.
Let's understand each operation.
The Peek operation returns the element at the front of the queue without removing it.
It's useful when you only need to know which element will be processed next.
Syntax
REAR (queue)
IF front == -1 THEN
PRINT "Queue Underflow"
ELSE
RETURN queue[rear]
END IF
For example, if the queue contains:
[15] [25] [35] [ ]
The Peek operation returns 15 because it is the first element in the queue.
Since no insertion or deletion occurs, both the front and rear pointers remain unchanged.
The Rear operation returns the most recently inserted element.
Syntax
REAR(queue)
IF front == -1 THEN
PRINT "Queue Underflow"
ELSE
RETURN queue[rear]
END IF
Suppose the queue contains:
[15] [25] [35] [45]
The Rear operation returns 45 because it is stored at the rear of the queue.
Like Peek, this operation doesn't modify the queue.
Before removing an element, the program should always verify whether the queue contains any data.
If both pointers indicate that no elements exist, the queue is considered empty.
Syntax
ISEMPTY()
IF front == -1 OR front > rear THEN
RETURN TRUE
ELSE
RETURN FALSE
END IF
This simple check prevents invalid dequeue operations and is a standard part of every queue implementation using array in data structure.
Since an array has a fixed capacity, the program should also check whether all positions are occupied before inserting a new element.If the rear pointer has reached the last available index, the queue is full.
Syntax
ISFULL()
IF rear == MAX - 1 THEN
RETURN TRUE
ELSE
RETURN FALSE
END IF
Attempting another insertion without checking this condition causes a queue overflow.
These validation operations make the implementation queue using array more reliable and easier to debug.
Also read : linked list implementation in C
Enqueue and Dequeue are the two most important operations in a queue. Every other operation supports or validates these two.
Understanding them is the key to mastering queue implementation using array.
The Enqueue operation inserts a new element at the rear of the queue.
Before insertion, the program checks whether the queue has enough space.
If space is available:
Suppose the queue is empty.
[ ] [ ] [ ] [ ] [ ]
Insert 10.
[10] [ ] [ ] [ ] [ ]
Front = 0
Rear = 0
Insert 20.
[10] [20] [ ] [ ] [ ]
Front = 0
Rear = 1
Only the rear pointer moves because new elements are always added at the end.
This behavior remains the same in every implementation of a queue using array, regardless of the programming language.
The Dequeue operation removes the element at the front of the queue.
Unlike Enqueue, it works from the opposite end.
Before removing an element, the program verifies that the queue isn't empty.
Consider this queue.
[10] [20] [30] [40] [ ]
Front = 0
Rear = 3
After one Dequeue operation, 10 is removed.
Logically, the queue becomes:
[ ] [20] [30] [40] [ ]
Front = 1
Rear = 3
The remaining elements don't shift to the left.Instead, only the front pointer moves forward.
This makes deletion efficient and keeps the implementation of queue using array in C simple because unnecessary data movement is avoided.
The following example shows how a queue changes after each operation.
This simple flow clearly shows how insertion and deletion affect the front and rear pointers while preserving the FIFO order.
Also read : stack implementation using linked lists
Now let's put everything together with a simple example. Assume an array with a capacity of five elements.
Initially, the queue is empty.
Front |
Rear |
Queue |
-1 |
-1 |
[ ] [ ] [ ] [ ] [ ] |
Initial Queue State
Insert 5.
Front |
Rear |
Queue |
0 |
0 |
[5] [ ] [ ] [ ] [ ] |
Next, insert 15 and 25.
Front |
Rear |
Queue |
0 |
2 |
[5] [15] [25] [ ] [ ] |
The queue now contains three elements in the order they were inserted.
Performing Dequeue
Remove one element.
Front |
Rear |
Queue |
1 |
2 |
[ ] [15] [25] [ ] [ ] |
The value 5 is removed because it entered the queue first.
The remaining elements stay in their original positions, while only the front pointer changes.
Final Queue State
Insert 35.
Front |
Rear |
Queue |
1 |
3 |
[ ] [15] [25] [35] [ ] |
The following program combines all the steps discussed above into a single example. It performs enqueue and dequeue operations in sequence, allowing you to observe how the front and rear pointers change as the queue is updated.
#include <stdio.h>
#define SIZE 5
// Queue structure
struct Queue {
int items[SIZE];
int front;
int rear;
};
// Initialize queue
void initialize(struct Queue *q) {
q->front = -1;
q->rear = -1;
}
// Enqueue operation
void enqueue(struct Queue *q, int value) {
if (q->rear == SIZE - 1) {
printf("Queue Overflow\n");
return;
}
if (q->front == -1)
q->front = 0;
q->items[++q->rear] = value;
}
// Dequeue operation
void dequeue(struct Queue *q) {
if (q->front == -1 || q->front > q->rear) {
printf("Queue Underflow\n");
return;
}
printf("Dequeued Element: %d\n", q->items[q->front]);
q->front++;
if (q->front > q->rear) {
q->front = -1;
q->rear = -1;
}
}
// Display queue
void display(struct Queue *q) {
int i;
if (q->front == -1) {
printf("Queue is Empty\n");
return;
}
printf("Queue: ");
for (i = q->front; i <= q->rear; i++)
printf("%d ", q->items[i]);
printf("\n");
}
int main() {
struct Queue q;
initialize(&q);
// Insert 5, 15, and 25
enqueue(&q, 5);
enqueue(&q, 15);
enqueue(&q, 25);
display(&q);
// Remove one element
dequeue(&q);
display(&q);
// Insert 35
enqueue(&q, 35);
display(&q);
return 0;
}
Output
Queue: 5 15 25
Dequeued Element: 5
Queue: 15 25
Queue: 15 25 35
This example demonstrates the complete working of the queue implementation using array in data structures . Although the queue appears simple, every insertion and deletion follows the same FIFO rule. Once you understand these operations, implementing the data structure in code becomes much easier.
Ready to move beyond programming fundamentals? upGrad's Executive Diploma in Data Science & Artificial Intelligence from IIITB equips you with practical expertise in Python, data science, machine learning, and artificial intelligence through an industry-focused curriculum and project-based learning.
The front and rear pointers are the backbone of queue implementation using array. Without them, the program wouldn't know where to insert a new element or which element should be removed next.
Think of these pointers as markers rather than data. They don't store queue elements. Instead, they keep track of the queue's current boundaries inside the array.
Once you understand how these pointers move, the implementation of queue using array becomes much easier to visualize and debug.
The front pointer always points to the first valid element in the queue.
Whenever a dequeue operation is performed, the front pointer moves one position ahead.
For example, consider the following queue.
Queue |
Front |
Rear |
| [10] [20] [30] [ ] [ ] | 0 |
2 |
After removing 10, the queue logically becomes:
Queue |
Front |
Rear |
| [ ] [20] [30] [ ] [ ] | 1 |
2 |
Notice that only the front pointer changes. The remaining elements stay where they are.
This approach avoids unnecessary shifting of data and keeps queue operations efficient.
The rear pointer always identifies the last inserted element.
Whenever a new value is added, the rear pointer moves to the next available position.
Example:
Operation |
Queue |
Rear |
| Enqueue 10 | [10] [ ] [ ] [ ] [ ] | 0 |
| Enqueue 20 | [10] [20] [ ] [ ] [ ] | 1 |
| Enqueue 30 | [10] [20] [30] [ ] [ ] | 2 |
The rear pointer grows with every insertion, while the front pointer remains unchanged until a deletion occurs.
This pointer movement is identical in every implementation of queue using array in C and other programming languages.
Also read : Difference between LinkedList and Array in Java
The movement of both pointers follows a simple pattern.
Operation |
Front |
Rear |
| Initialize | -1 |
-1 |
| First Enqueue | 0 |
0 |
| Next Enqueue | 0 |
1 |
| Another Enqueue | 0 |
2 |
| Dequeue | 1 |
2 |
| Next Dequeue | 2 |
2 |
Only one pointer changes during each operation.
This predictable behavior makes queue implementation using array in data structure easier to understand and implement.
Pointer-related mistakes are common, especially when you're implementing a queue for the first time.
Some of the most frequent errors include:
Testing these edge cases helps you build a more reliable queue implementation.
A linear queue is the simplest form of queue implementation using array. Elements are inserted from the rear and removed from the front. Once the rear pointer reaches the last index of the array, no more elements can be inserted.
It looks simple. Yet it has one important drawback.
Consider an array with five positions.
Step |
Queue |
Front |
Rear |
| Initial | [ ] [ ] [ ] [ ] [ ] | -1 |
-1 |
| Enqueue 10 | [10] [ ] [ ] [ ] [ ] | 0 |
0 |
| Enqueue 20 | [10] [20] [ ] [ ] [ ] | 0 |
1 |
| Enqueue 30 | [10] [20] [30] [ ] [ ] | 0 |
2 |
The queue continues to grow until the last index is reached.
These benefits make the implementation queue using array an excellent learning exercise for beginners.
Now consider this sequence.
The queue becomes:
Queue |
Front |
Rear |
| [ ] [ ] [ ] [40] [50] | 3 |
4 |
Although the first three positions are empty, new elements can't be inserted because the rear pointer has already reached the last index.
This unused space is called memory wastage.
It's the biggest drawback of a linear queue and the main reason circular queues were introduced.
A circular queue solves the unused space problem found in a linear queue.
Instead of stopping at the last array position, the rear pointer wraps around to the beginning whenever free space becomes available.
This small improvement makes a big difference.
A standard queue implemented using an array cannot reuse the empty spaces created after dequeue operations. As a result, the queue may appear full even when unused positions are available. The following example illustrates why a circular queue is needed to solve this limitation.
Queue |
Front |
Rear |
| [ ] [ ] [30] [40] [50] | 2 |
4 |
The first two positions are empty. A linear queue can't use them.
A circular queue can. The rear pointer simply returns to index 0 and starts inserting elements again.
The queue behaves like a circle instead of a straight line.
Pointer movement follows this pattern:
0 → 1 → 2 → 3 → 4
↑ ↓
└─────────────────┘
Whenever the rear reaches the last index, it checks whether free space exists at the beginning.
If it does, insertion continues from there.
This makes much better use of available memory.
Both linear and circular queues follow the FIFO principle, but they differ in how they utilize available memory. The comparison below highlights their key differences in structure, efficiency, and space utilization.
Feature |
Linear Queue |
Circular Queue |
| Memory usage | Wastes space | Efficient |
| Pointer movement | One direction | Circular |
| Overflow | Occurs sooner | Delayed until queue is actually full |
| Implementation | Simple | Slightly more complex |
If your application performs frequent enqueue and dequeue operations, a circular queue is usually the better choice.
Two common errors can occur during queue implementation using array.
They are overflow and underflow. Understanding these conditions helps prevent runtime errors and unexpected behavior.
Overflow occurs when an insertion is attempted after the queue becomes full.
Example:
Capacity = 5
Current Queue:
[10] [20] [30] [40] [50]
Trying to insert another element isn't possible because every position is already occupied.
The program should detect this condition before performing the insertion.
Follow these simple practices.
These checks make the implementation of queue using array more reliable and easier to maintain.
Also read : Implementation of Queue Using Linked List in C with Code Example
Choosing between an array and a linked list depends on how your application stores and manages data. Both follow the FIFO principle, but they differ in memory allocation, flexibility, and performance.
If you're learning queue implementation using array, it's useful to understand where arrays perform well and where linked lists have an advantage.
Comparison Between Queue Using Array and Queue Using Linked List :
Feature |
Queue Using Array |
Queue Using Linked List |
| Memory allocation | Fixed size | Dynamic size |
| Memory usage | May waste unused space | Uses memory only when needed |
| Overflow | Possible when array is full | Limited only by available memory |
| Cache performance | Better due to contiguous memory | Slightly slower because nodes are scattered |
| Implementation | Easier for beginners | More complex |
| Best suited for | Fixed-size applications | Frequently changing queue sizes |
An array-based queue is a good choice when:
Examples include printer queues, keyboard buffers, and embedded systems where memory usage is predefined.
A linked-list queue is more suitable when:
Although linked lists offer greater flexibility, the implementation of queue using array is still preferred in many learning environments because it's easier to understand and implement.
Also read : Stack Implementation Using Array in Data Structures
Like every data structure, queue implementation using array has strengths and limitations. Knowing both helps you decide whether it's the right choice for your application.
The implementation of queue using array offers several benefits.
These advantages make the implementation queue using array one of the first queue implementations taught in data structure courses.
Despite its simplicity, an array-based queue has a few drawbacks.
For applications with unpredictable workloads, a linked-list queue or a circular queue is often a better alternative.
Also read : Types of Queue
Writing a queue isn't only about making it work. Good implementations are easier to understand, maintain, and debug.
Keep these best practices in mind.
These practices make the implementation of queue using array in C cleaner and reduce common programming mistakes.
Also read: Doubly Linked List Data Structures: A Complete Guide
Learning queue implementation using array gives you a strong foundation for understanding queues and other linear data structures. By using an array along with front and rear pointers, you can efficiently insert and remove elements while maintaining the FIFO order.
Although a simple array-based queue has limitations such as fixed capacity and memory wastage in linear queues, it's still one of the easiest ways to understand queue operations. Once you're comfortable with this approach, you'll find it much easier to explore circular queues, linked-list implementations, and more advanced data structures.
Ready to start your journey? Book a free consultation with upGrad today to find the best path for your career.
Yes, a queue can be implemented using an array by storing elements in sequential memory locations and managing them with front and rear pointers. The implementation of queue using array is simple, efficient, and widely used when the maximum queue size is known before execution.
No. Queues can be implemented using arrays, linked lists, or other dynamic data structures. Queue implementation using array in data structure is preferred for fixed-size queues because it offers fast access, while linked lists are better suited for applications where the queue size changes frequently.
The four commonly used queue types are linear queue, circular queue, priority queue, and deque (double-ended queue). Each serves a different purpose. While the implementation queue using array commonly demonstrates linear and circular queues, other types use different insertion and deletion rules.
The biggest limitation is fixed capacity, which can lead to queue overflow when the array becomes full. A linear queue may also leave unused memory after several dequeue operations. These drawbacks are important considerations when choosing the implementation of queue using array for large applications.
Array implementation refers to storing data elements in contiguous memory locations while accessing them through indexes. Many linear data structures, including stacks and queues, rely on this approach because it provides predictable memory allocation and efficient element access.
The best implementation depends on the application's requirements. If the queue size is fixed and performance is important, the implementation of queue using array in C or other programming languages works well. For dynamic workloads, linked-list implementations usually offer greater flexibility.
Although both use arrays for storage, they follow different processing orders. A stack works on the LIFO (Last In, First Out) principle, while a queue follows FIFO (First In, First Out). As a result, the logic for insertion and deletion is completely different.
Queues always follow the FIFO (First In, First Out) principle, meaning the first element inserted is the first one removed. This ordering makes queues suitable for scheduling systems, buffering tasks, and request processing where operations must occur in arrival order.
For interviews, candidates are often expected to understand both queue implementation using array and linked-list implementations. Interviewers usually focus on pointer management, overflow and underflow handling, time complexity, and choosing the right implementation for different problem scenarios.
A priority queue is usually implemented using a binary heap because it provides efficient insertion and deletion based on priority. Although arrays can represent a priority queue, heap-based implementations are generally preferred for better performance in practical applications.
You should avoid an array-based queue when the number of elements isn't predictable or when frequent resizing is required. In such cases, linked-list queues provide dynamic memory allocation and eliminate the fixed-capacity limitation found in the implementation of queue using array.
650 articles published
Sriram K is a Senior SEO Executive with a B.Tech in Information Technology from Dr. M.G.R. Educational and Research Institute, Chennai. With over a decade of experience in digital marketing, he specia...
Speak with Data Science Expert
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources