Data Structure • Chapter 32 • Engineering Blueprint
Queue Implementation Using Array and Linked List in C: Enqueue, Dequeue, Algorithms and Examples
Two different memory structures, one FIFO rule. Learn to implement a queue using array indices and linked-list pointers with complete C programs, dry runs and step-by-step explanations.
You have already learned that a queue follows the First In, First Out (FIFO) principle. But a new question comes when you start programming: How do we actually store a queue in computer memory?
Should we use an array? Should we use a linked
list? How does Front change after deletion?
What happens when the queue becomes empty?
And why do we need malloc() and
free() in a linked queue?
In this chapter, we will answer these questions in very simple language. We will build both implementations from the beginning, understand each statement of the C programs and compare their performance.
Queue implementation using Array and Linked List means creating a FIFO data structure using either contiguous array storage or dynamically connected nodes.
In an array queue, Front and Rear are usually integer indices. In a linked-list queue, Front and Rear are pointers to the first and last nodes. With suitable implementations, Enqueue and Dequeue both take O(1) time.
What Does Queue Implementation Mean?

Implementation means converting a data structure concept into a working program. We already know that a normal queue inserts elements at Rear and removes them from Front.
Now we need to decide how those values will be stored in memory.
Queue Using Array
Elements are stored in numbered array positions. Front and Rear keep track of the relevant indices.
Example:
data[0] = 10
Queue Using Linked List
Values are stored inside nodes. A pointer in each node connects it to the next node.
Example:
front->data
Basic Queue Operations
- Enqueue: Add a value at Rear.
- Dequeue: Remove a value from Front.
- Peek: Read Front without deleting.
- Display: Show all queue elements.
- isEmpty: Check whether the queue contains any elements.
If you want to revise FIFO, overflow, underflow and queue types before beginning, read the previous chapter: Queue in Data Structure: FIFO Principle, Types and Operations .
Queue Implementation Using Array in C
An array stores values in contiguous memory positions. For example, we may create an integer array with five positions.
We identify these positions using indices 0, 1, 2, 3 and 4.
To turn this array into a queue, we maintain information about its Front and Rear.
Important Array Queue Variables
Our C implementation will use the following variables:
| Variable | Purpose | Example |
|---|---|---|
items[] |
Stores queue elements | 10, 20, 30 |
front |
Index of oldest element | 0 |
rear |
Index of newest element | 2 |
count |
Number of stored elements | 3 |
CAPACITY |
Maximum number of active values | 5 |
Linear Array Queue vs Circular Array Queue
In a simple linear queue, Rear only moves forward. After repeated deletions, empty positions at the beginning may remain unused. This situation can cause false overflow.
In a circular array queue, indices wrap from the last position back to the beginning. It can reuse the earlier free positions.
next = (current + 1) % CAPACITY
If capacity is 5 and the current index is 4, the next index becomes 0.
We will use a circular array in the complete C program because it handles repeated Enqueue and Dequeue operations more effectively than a basic non-circular array queue.
Algorithms for Queue Using Array
Before writing the program, understand the operations in plain steps.
Algorithm: Enqueue in Circular Array Queue
- Check whether Count equals Capacity.
- If full, report overflow.
- Otherwise calculate the next Rear index using modulo.
- Store the new value at Rear.
- Increase Count by one.
ENQUEUE(queue, value):
IF count == CAPACITY:
PRINT "Overflow"
RETURN
rear = (rear + 1) % CAPACITY
items[rear] = value
count = count + 1
Algorithm: Dequeue in Circular Array Queue
- Check whether Count is zero.
- If empty, report underflow.
- Otherwise read the value at Front.
- Move Front forward using modulo.
- Decrease Count by one.
- If the queue becomes empty, reset the Front and Rear positions.
DEQUEUE(queue):
IF count == 0:
PRINT "Underflow"
RETURN
value = items[front]
front = (front + 1) % CAPACITY
count = count - 1
IF count == 0:
front = 0
rear = -1
RETURN value
For a beginner explanation of algorithms and pseudocode, see Algorithm in Data Structure: Characteristics, Steps and Examples .
Queue Using Array in C — Complete Program
Now we will implement the same circular array queue using C.
The program contains functions for initialization, Enqueue, Dequeue, Peek and Display.
#include <stdio.h>
#define CAPACITY 5
typedef struct {
int items[CAPACITY];
int front;
int rear;
int count;
} Queue;
void init(Queue *q) {
q->front = 0;
q->rear = -1;
q->count = 0;
}
int enqueue(Queue *q, int value) {
if (q->count == CAPACITY) {
puts("Array queue full");
return 0;
}
q->rear = (q->rear + 1) % CAPACITY;
q->items[q->rear] = value;
q->count++;
return 1;
}
int dequeue(Queue *q, int *out) {
if (q->count == 0) {
puts("Array queue empty");
return 0;
}
*out = q->items[q->front];
q->front = (q->front + 1) % CAPACITY;
q->count--;
if (q->count == 0) {
init(q);
}
return 1;
}
int peek(const Queue *q, int *out) {
if (q->count == 0) {
return 0;
}
*out = q->items[q->front];
return 1;
}
void display(const Queue *q) {
if (q->count == 0) {
puts("Empty queue");
return;
}
printf("Queue: ");
for (int i = 0; i < q->count; i++) {
int index = (q->front + i) % CAPACITY;
printf("%d ", q->items[index]);
}
putchar('\n');
}
int main(void) {
Queue q;
int value;
init(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
if (dequeue(&q, &value)) {
printf("Removed: %d\n", value);
}
enqueue(&q, 40);
enqueue(&q, 50);
enqueue(&q, 60);
if (peek(&q, &value)) {
printf("Front: %d\n", value);
}
display(&q);
if (dequeue(&q, &value)) {
printf("Removed: %d\n", value);
}
enqueue(&q, 70);
display(&q);
return 0;
}
Output of Array Queue Program
Removed: 10
Front: 20
Queue: 20 30 40 50 60
Removed: 20
Queue: 30 40 50 60 70
Understanding the Program
1. Structure:
The Queue structure stores
the array, Front, Rear and Count together.
2. Initialization: The queue begins empty, so Count is zero. Rear is initially -1.
3. Enqueue: We check capacity, move Rear and store the value at the new Rear position.
4. Dequeue: We return the Front value, move Front and decrease Count.
5. Display: We print elements in logical FIFO order, even if their physical array indices have wrapped around.
int *out so the removed value
can be returned through it.
This also lets the function report
success separately using its integer
return value.
Array Queue Dry Run: Front and Rear Changes
A dry run means checking the program step by step without depending only on the final output.
Consider a circular queue of capacity 5.
| Step | Operation | Front | Rear | Count | Queue |
|---|---|---|---|---|---|
| 1 | Enqueue 10 | 0 | 0 | 1 | 10 |
| 2 | Enqueue 20 | 0 | 1 | 2 | 10, 20 |
| 3 | Enqueue 30 | 0 | 2 | 3 | 10, 20, 30 |
| 4 | Dequeue 10 | 1 | 2 | 2 | 20, 30 |
| 5 | Enqueue 40 | 1 | 3 | 3 | 20, 30, 40 |
| 6 | Enqueue 50 | 1 | 4 | 4 | 20, 30, 40, 50 |
| 7 | Enqueue 60 | 1 | 0 | 5 | 20, 30, 40, 50, 60 |
| 8 | Dequeue 20 | 2 | 0 | 4 | 30, 40, 50, 60 |
| 9 | Enqueue 70 | 2 | 1 | 5 | 30, 40, 50, 60, 70 |
Why Does Rear Become 0 After Index 4?
Because this is a circular array queue.
(4 + 1) % 5 = 0
Position 0 was already released when 10 was removed. So 60 can be stored there without changing the logical FIFO order.
Queue Implementation Using Linked List in C
A linked-list queue stores each value inside a separate node.
Every node contains two things:
- Data: The value stored in the node.
- Next: A pointer to the next node.
Unlike an array, linked-list nodes do not need to occupy consecutive memory locations.
Why Do We Need Two Pointers?
Front identifies the first node, which will be removed during Dequeue.
Rear identifies the last node, where a new node can be attached.
Keeping both pointers lets us add and remove nodes in constant time without traversing the complete list.
Understanding malloc() and free()
malloc() allocates memory
for a new node while the program is running.
free() releases memory when
that node is no longer needed.
If malloc() fails, it returns
NULL. A correct implementation checks
this before using the new pointer.
To understand pointers and node storage in more detail, revise Linked List Representation Using Arrays and Pointers .
Linked List Queue Enqueue and Dequeue Algorithms
Algorithm: Enqueue Using Linked List
- Allocate a new node.
- Check whether memory allocation succeeded.
- Store the new value inside the node.
- Set its Next pointer to NULL.
- If the queue is empty, make Front point to the new node.
- Otherwise link the old Rear node to the new node.
- Update Rear to the new node.
ENQUEUE(value):
newNode = ALLOCATE NODE
IF newNode == NULL:
PRINT "Allocation failed"
RETURN
newNode.data = value
newNode.next = NULL
IF rear != NULL:
rear.next = newNode
ELSE:
front = newNode
rear = newNode
Algorithm: Dequeue Using Linked List
- Check whether Front is NULL.
- If empty, report underflow.
- Save the current Front node in a temporary pointer.
- Read its data.
- Move Front to the next node.
- If Front becomes NULL, also set Rear to NULL.
- Free the removed node.
DEQUEUE():
IF front == NULL:
PRINT "Underflow"
RETURN
temp = front
value = temp.data
front = front.next
IF front == NULL:
rear = NULL
FREE temp
RETURN value
Queue Using Linked List in C — Complete Program
Now let us implement the linked queue in C using structures, pointers, malloc and free.
This program includes safe handling for empty queues and allocation failures. It also releases remaining nodes before the program finishes.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
typedef struct {
Node *front;
Node *rear;
size_t count;
} Queue;
void init(Queue *q) {
q->front = NULL;
q->rear = NULL;
q->count = 0;
}
int enqueue(Queue *q, int value) {
Node *node = malloc(sizeof *node);
if (node == NULL) {
puts("Allocation failed");
return 0;
}
node->data = value;
node->next = NULL;
if (q->rear != NULL) {
q->rear->next = node;
} else {
q->front = node;
}
q->rear = node;
q->count++;
return 1;
}
int dequeue(Queue *q, int *out) {
if (q->front == NULL) {
puts("Linked queue empty");
return 0;
}
Node *old = q->front;
*out = old->data;
q->front = old->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(old);
q->count--;
return 1;
}
int peek(const Queue *q, int *out) {
if (q->front == NULL) {
return 0;
}
*out = q->front->data;
return 1;
}
void display(const Queue *q) {
printf("Queue: ");
if (q->front == NULL) {
puts("empty");
return;
}
for (const Node *p = q->front;
p != NULL;
p = p->next) {
printf("%d ", p->data);
}
putchar('\n');
}
void clear(Queue *q) {
while (q->front != NULL) {
Node *old = q->front;
q->front = old->next;
free(old);
}
q->rear = NULL;
q->count = 0;
}
int main(void) {
Queue q;
int value;
init(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
if (dequeue(&q, &value)) {
printf("Removed: %d\n", value);
}
enqueue(&q, 40);
enqueue(&q, 50);
enqueue(&q, 60);
if (peek(&q, &value)) {
printf("Front: %d\n", value);
}
display(&q);
if (dequeue(&q, &value)) {
printf("Removed: %d\n", value);
}
enqueue(&q, 70);
display(&q);
clear(&q);
return 0;
}
Output of Linked List Queue Program
Removed: 10
Front: 20
Queue: 20 30 40 50 60
Removed: 20
Queue: 30 40 50 60 70
Program Explanation
Node structure: Every node stores a data value and a Next pointer.
Queue structure: Keeps track of Front, Rear and Count.
Enqueue: Allocates a new node and attaches it after the current Rear.
Dequeue: Removes the old Front node, updates the pointer and frees the memory.
Peek: Reads the first node without deleting it.
Display: Traverses nodes from Front until NULL.
Clear: Frees all remaining nodes to avoid memory leaks.
Nodes are allocated one at a time. The linked queue can grow until memory allocation fails or some application-defined limit is reached.
Linked Queue Dry Run with Pointer Changes
Let us understand how Front and Rear behave when we insert and delete nodes.
| Step | Operation | Front Data | Rear Data | Queue |
|---|---|---|---|---|
| 1 | Enqueue 10 | 10 | 10 | 10 |
| 2 | Enqueue 20 | 10 | 20 | 10, 20 |
| 3 | Enqueue 30 | 10 | 30 | 10, 20, 30 |
| 4 | Dequeue 10 | 20 | 30 | 20, 30 |
| 5 | Enqueue 40 | 20 | 40 | 20, 30, 40 |
| 6 | Dequeue 20 | 30 | 40 | 30, 40 |
What Happens When the Only Node Is Removed?
Suppose the queue contains only 10. Both Front and Rear point to that node.
After Dequeue, Front moves to NULL because the removed node has no successor. Rear must also become NULL.
Before Dequeue
Front → 10 → NULL
Rear → 10
After Dequeue
Front = NULL
Rear = NULL
Interactive Queue Implementation Comparison
Both implementations follow FIFO, but their storage behaves differently. Use this lab to try the same operations in Array and Linked List mode.
Array mode uses five circular positions. Linked List mode shows connected nodes. The linked-list demonstration has a 30-element safety limit only to keep the visual interface manageable; the C linked-list program is not restricted to that number.
Queue Implementation Lab
3 elementsF = Front. R = Rear. Empty slots use a dash. Both modes have separate states.
Start: Front = 10, Rear = 30.
Try this: Remove 10, insert 40, then compare the result in both modes. The logical order stays the same.
Difference Between Array Queue and Linked List Queue
This comparison is especially useful for 5-mark and 10-mark university questions.
| Basis | Array Implementation | Linked List Implementation |
|---|---|---|
| Storage | Contiguous array elements | Connected dynamic nodes |
| Size | Fixed capacity in our program | Grows by allocating new nodes |
| Front and Rear | Integer indices | Node pointers |
| Memory Allocation | Array reserved in Queue structure | malloc creates nodes |
| Memory Release | No node-by-node free required | free each removed node |
| Enqueue | O(1) | O(1) |
| Dequeue | O(1) | O(1) |
| Memory Overhead | May reserve unused array slots | Additional Next pointer per node |
| Locality | Usually better cache locality | Nodes may be scattered |
| Capacity Limit | Array can become full | Limited by available memory |
| Common Mistake | Incorrect circular indexing | Incorrect pointer update or memory leak |
Which Implementation Should You Choose?
Choose an array queue when you want a simple fixed-capacity structure with predictable storage and good memory locality.
Choose a linked-list queue when the number of elements may change frequently and you do not want to reserve a fixed-size array.
However, dynamic allocation has its own overhead, and the correct choice depends on the application.
Time and Space Complexity of Both Implementations
Time complexity describes how the running time grows with input size. Space complexity describes how much storage an algorithm needs.
| Operation | Circular Array Queue | Linked List Queue |
|---|---|---|
| Enqueue | O(1) | O(1) |
| Dequeue | O(1) | O(1) |
| Peek | O(1) | O(1) |
| Check Empty | O(1) | O(1) |
| Display n Elements | O(n) | O(n) |
| Total Storage | O(CAPACITY) | O(n) |
| Extra Work Space per Enqueue | O(1) | O(1) working space, plus a new node |
Why Is Enqueue O(1) in Array Queue?
Because we calculate the next Rear position and store a value directly. We do not traverse all the elements.
Why Is Enqueue O(1) in Linked List Queue?
Because Rear already points to the last node. We can attach the new node directly without searching.
The time analysis treats one memory allocation as a constant-time operation in the usual abstract algorithm model. Real allocation costs can vary.
Why Is Display O(n)?
Because we must visit each of the n elements to print the complete queue.
Common Programming Mistakes in Queue Implementation
Array Queue Mistakes
- Forgetting overflow checks.
- Confusing Front and Rear indices.
- Using wrong modulo calculations.
- Not resetting indices when empty.
- Displaying by raw array order after wrapping.
Linked Queue Mistakes
- Forgetting malloc failure checks.
- Not setting newNode Next to NULL.
- Not updating Rear after insertion.
- Leaving Rear pointing to a freed node.
- Forgetting free and causing memory leaks.
Important Edge Cases
- Dequeue from an empty queue.
- Insert the first element.
- Remove the last remaining element.
- Attempt to insert into a full array queue.
- Insert after array indices wrap around.
- Handle memory-allocation failure.
Practical Applications of Array and Linked List Queues
Array Queue Applications
- Fixed-capacity task buffers.
- Embedded systems with bounded memory.
- Circular data buffers.
- Repeated producer-consumer processing.
Linked Queue Applications
- Dynamically growing task lists.
- Linked work-request queues.
- Graph breadth-first search.
- Applications requiring flexible node counts.
These examples show a general principle of Data Structure design: choose the implementation according to memory, capacity and workload requirements.
A Linked List itself is explained in detail in the earlier chapter Linked List in Data Structure: Types, Operations and Examples .
Important Exam and PYQ-Style Questions with Answers
These are original PYQ-style practice questions. They are not claimed to be verified questions from any particular university examination.
Q1. What Is Queue Implementation?
Answer: Queue implementation is the process of creating a FIFO data structure using a storage method such as an array or linked list.
Q2. What Is the Role of Front and Rear?
Answer: Front identifies the next element to be removed, while Rear identifies the position or node where insertion occurs.
Q3. Why Is malloc() Used in Linked Queue?
Answer: malloc allocates memory dynamically for a new node during queue insertion.
Q4. What Is the Time Complexity of Enqueue?
Answer: O(1) in the circular array implementation and in the linked-list implementation with a maintained Rear pointer.
Q5. Explain Queue Implementation Using Array.
Answer: In an array queue, elements are stored in indexed memory positions. Front identifies the first active element and Rear identifies the last inserted element.
Enqueue inserts at Rear and Dequeue removes from Front. In a circular implementation, positions are calculated using modulo to reuse available slots. Enqueue and Dequeue take O(1) time.
Q6. Explain Queue Implementation Using Linked List.
Answer: A linked-list queue stores values in nodes containing Data and Next fields. Front points to the first node and Rear points to the last node.
Enqueue allocates a new node using malloc and attaches it after Rear. Dequeue removes the Front node, updates Front and releases memory using free.
Both operations take O(1) time with maintained Front and Rear pointers, under the usual constant-time allocation assumption.
Q7. Differentiate Array Queue and Linked Queue.
Answer: An array queue uses contiguous storage and integer indices. A linked queue uses dynamically allocated nodes connected by pointers.
A fixed array queue has predefined capacity, while a linked queue can grow until allocation fails. Array queues generally provide better cache locality, while linked queues require extra pointer memory.
Q8. Explain Queue Implementation Using Array and Linked List with Algorithms and Complexity.
Answer: Queue is a linear data structure that follows the FIFO principle. Queue implementation can be performed using an array or a linked list.
Array Method: A fixed-size array stores the elements. Front and Rear indices maintain the positions of active values. In a circular queue, modulo arithmetic allows released slots to be reused.
Linked Method: A linked list stores elements in dynamically allocated nodes. Every node contains Data and Next. Front points to the first node and Rear to the last node.
In both implementations, Enqueue adds an element at Rear and Dequeue removes the Front element.
With suitable implementations, Enqueue, Dequeue and Peek take O(1) time, while displaying all n elements takes O(n) time.
Array storage depends on the defined capacity, whereas linked storage depends on the number of allocated nodes. Both complete C programs are given earlier in this chapter.
Practice Questions, FAQs and Quick Revision
Q1. Which data structure follows FIFO?
Answer: Queue.
Q2. Which pointer is updated during linked-list Enqueue?
Answer: Rear is updated to the new node. If the queue was empty, Front also points to that node.
Q3. What happens when the last linked-list node is deleted?
Answer: Front and Rear both become NULL.
Q4. Why do we use modulo in a circular queue?
Answer: To wrap indices from the last array position to the beginning.
Q5. Does a linked queue have unlimited memory?
Answer: No. It can grow dynamically, but available memory and application limits still restrict its size.
Q6. Which implementation is faster?
Answer: Both normally provide O(1) Enqueue and Dequeue. Actual performance depends on memory locality, allocation costs, capacity and workload.
Q7. Why is free() important?
Answer: It releases memory belonging to removed dynamically allocated nodes.
Q8. What is the complexity of Display?
Answer: O(n), because every active queue element must be visited.
One-Minute Revision
Conclusion
Queue implementation becomes easy when you understand one important point: the FIFO rule remains the same, but memory storage changes.
An array queue uses indexed storage, while a linked queue uses connected nodes. Both can support efficient Enqueue and Dequeue operations.
For university exams, practise the array Front/Rear dry run, draw the linked-node diagram, understand malloc and free, and remember to update both pointers when the final node is removed.
Once these concepts are clear, implementing queues in larger algorithms becomes much easier.
