Queue using array
An array-based queue stores elements in contiguous memory and tracks two indices: front (next to remove) and rear (last inserted). The best practical fixed-size design is the circular queue, where indices wrap with modulo and both enqueue and dequeue are O(1).
On this page
- Representation — array, front/rear, size, capacity
- Operations — enqueue/dequeue/peek with wrap-around
- Example in C — circular queue implementation
- Trade-offs — fixed capacity vs linked list
1) Array representation
To avoid costly shifts, use circular indexing instead of moving all elements after every dequeue.
| Component | Role | Notes |
|---|---|---|
items[] | Stores queue elements. | Fixed length MAX. |
front | Index of next element to dequeue. | Valid when queue not empty. |
rear | Index of last inserted element. | Updated on enqueue with wrap-around. |
size | Current number of elements. | Empty when size == 0, full when size == MAX. |
2) Mapping operations
| Operation | Array steps | Failure case |
|---|---|---|
enqueue(x) | if not full: rear=(rear+1)%MAX, store x, size++. | Overflow if full. |
dequeue() | if not empty: read items[front], front=(front+1)%MAX, size--. | Underflow if empty. |
peek() | if not empty: return items[front]. | Underflow if empty. |
isEmpty() | size == 0. | — |
isFull() | size == MAX. | — |
3) Example in C
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 // Maximum size of the queue
int queue[MAX];
int front = -1;
int rear = -1;
// Function to check if the queue is full
int isFull() {
return rear == MAX - 1;
}
// Function to check if the queue is empty
int isEmpty() {
return front == -1 || front > rear;
}
// Function to add an element to the queue (Enqueue)
void enqueue(int value) {
if (isFull()) {
printf("Queue Overflow! Cannot insert %d\n", value);
return;
}
if (front == -1) {
front = 0; // Set front to 0 when inserting first element
}
rear++;
queue[rear] = value;
printf("Enqueued: %d\n", value);
}
// Function to remove an element from the queue (Dequeue)
void dequeue() {
if (isEmpty()) {
printf("Queue Underflow! Queue is empty\n");
return;
}
printf("Dequeued: %d\n", queue[front]);
front++;
// Reset queue if it becomes empty
if (front > rear) {
front = -1;
rear = -1;
}
}
// Function to get the front element without removing it
int peek() {
if (isEmpty()) {
printf("Queue is empty\n");
return -1;
}
return queue[front];
}
// Function to display all elements in the queue
void display() {
if (isEmpty()) {
printf("Queue is empty\n");
return;
}
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
// Main function to test the queue
int main() {
printf("=== Queue Implementation using Array ===\n");
printf("Maximum size: %d\n\n", MAX);
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
display();
printf("\nFront element: %d\n", peek());
dequeue();
dequeue();
display();
enqueue(60); // This will show overflow (queue is full)
printf("\nFront element after dequeue: %d\n", peek());
return 0;
}
Execution Flow with Output Table
| Step | Operation | Action | Front Index | Rear Index | Queue State (front → rear) | Output Printed |
|---|---|---|---|---|---|---|
| 1 | enqueue(10) | Insert 10 | 0 | 0 | [10] | Enqueued: 10 |
| 2 | enqueue(20) | Insert 20 | 0 | 1 | [10, 20] | Enqueued: 20 |
| 3 | enqueue(30) | Insert 30 | 0 | 2 | [10, 20, 30] | Enqueued: 30 |
| 4 | enqueue(40) | Insert 40 | 0 | 3 | [10, 20, 30, 40] | Enqueued: 40 |
| 5 | enqueue(50) | Insert 50 | 0 | 4 | [10, 20, 30, 40, 50] | Enqueued: 50 |
| 6 | display() | Show all | 0 | 4 | [10, 20, 30, 40, 50] | Queue elements: 10 20 30 40 50 |
| 7 | peek() | View front | 0 | 4 | [10, 20, 30, 40, 50] | Front element: 10 |
| 8 | dequeue() | Remove front (10) | 1 | 4 | [20, 30, 40, 50] | Dequeued: 10 |
| 9 | dequeue() | Remove front (20) | 2 | 4 | [30, 40, 50] | Dequeued: 20 |
| 10 | display() | Show all | 2 | 4 | [30, 40, 50] | Queue elements: 30 40 50 |
| 11 | enqueue(60) | Try insert | 2 | 4 (MAX-1) | [30, 40, 50] | Queue Overflow! Cannot insert 60 |
| 12 | peek() | View front | 2 | 4 | [30, 40, 50] | Front element after dequeue: 30 |
Queue State Visualization Table
| After Operation | Index 0 | Index 1 | Index 2 | Index 3 | Index 4 | Front → Rear |
|---|---|---|---|---|---|---|
| Initial | empty | empty | empty | empty | empty | front = -1, rear = -1 |
After enqueue(10) | 10 | empty | empty | empty | empty | 0 → 0 |
After enqueue(20) | 10 | 20 | empty | empty | empty | 0 → 1 |
After enqueue(30) | 10 | 20 | 30 | empty | empty | 0 → 2 |
After enqueue(40) | 10 | 20 | 30 | 40 | empty | 0 → 3 |
After enqueue(50) | 10 | 20 | 30 | 40 | 50 | 0 → 4 |
After dequeue() | 20 | 30 | 40 | 50 | 1 → 4 | |
After dequeue() | 30 | 40 | 50 | 2 → 4 |
Note: strikethrough indicates deleted/ignored positions (wasted space).
Key Observations Table
| Concept | Value/Status | Explanation |
|---|---|---|
| Maximum Size | 5 | Queue can hold max 5 elements. |
| After 5 enqueues | Queue FULL | rear = 4 (MAX-1). |
| After 2 dequeues | front = 2 | First 2 positions (0,1) become unusable. |
isFull() after dequeues | TRUE (still) | rear is still 4, so insertion fails. |
| Wasted space | 2 positions | Index 0 and 1 are empty but cannot be reused. |
| Final queue content | [30, 40, 50] | Only 3 valid elements remain. |
Limitation Summary Table
| Problem | Cause | Consequence |
|---|---|---|
| Cannot insert 60 | rear = MAX-1 (4) | Even though front = 2 (space exists at 0,1). |
| Wasted memory | Deleted elements leave empty spaces | Memory inefficient for dynamic workloads. |
| False Overflow | isFull() checks only rear position | Queue reports FULL when actual elements < MAX. |
| Solution | Use Circular Queue | Reuses empty spaces by wrapping rear to 0. |
4) Trade-offs
- Pros — Simple, cache-friendly, O(1) operations with circular indexing.
- Cons — Fixed capacity unless resized manually.
Compare with Queue using linked list for dynamic growth, and Circular queue for deeper wrap-around discussion.
Key takeaway
Use a circular array queue to keep enqueue/dequeue O(1) without element shifting. Track front, rear, and size consistently.
Next up: Queue using dynamic array · Queue using linked list · Circular queue