Queue Implementation Using Array and Linked List in C: Enqueue, Dequeue, Algorithms and Examples

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.

Array Queue Linked Queue C Programs Exam Ready
Implementation Blueprint
FIFO Queue
↓
Array: Front and Rear Indices
Linked List: Front and Rear Pointers
↓
Enqueue / Dequeue / Peek

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.

Quick Answer

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.

01

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.

Implementation A

Queue Using Array

Elements are stored in numbered array positions. Front and Rear keep track of the relevant indices.

Example: data[0] = 10

Implementation B

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 .

Memory Trick — Same Rule, Different Storage: Array Queue = Indices. Linked Queue = Pointers. Both normally follow FIFO.
02

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.

Figure 1: Array Queue Memory Representation
Index 0 10 Front
Index 1 20
Index 2 30 Rear
Index 3 -
Index 4 -
Front Index = 0 Rear Index = 2 Number of Elements = 3
10 is the first value to leave. A new value such as 40 will join at the next available Rear position.

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.

Circular Index Formula:

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.

03

Algorithms for Queue Using Array

Before writing the program, understand the operations in plain steps.

Algorithm: Enqueue in Circular Array Queue

  1. Check whether Count equals Capacity.
  2. If full, report overflow.
  3. Otherwise calculate the next Rear index using modulo.
  4. Store the new value at Rear.
  5. Increase Count by one.
Algorithm 1 Enqueue
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

  1. Check whether Count is zero.
  2. If empty, report underflow.
  3. Otherwise read the value at Front.
  4. Move Front forward using modulo.
  5. Decrease Count by one.
  6. If the queue becomes empty, reset the Front and Rear positions.
Algorithm 2 Dequeue
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
Exam Memory Trick — CAME: Check capacity, Advance Rear, Make insertion, Extend Count.

For a beginner explanation of algorithms and pseudocode, see Algorithm in Data Structure: Characteristics, Steps and Examples .

04

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.

array_queue.c C11 Program
#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

Console Output
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.

Important C Concept: Dequeue uses an output pointer int *out so the removed value can be returned through it. This also lets the function report success separately using its integer return value.
05

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
1Enqueue 10 00110
2Enqueue 20 01210, 20
3Enqueue 30 02310, 20, 30
4Dequeue 10 12220, 30
5Enqueue 40 13320, 30, 40
6Enqueue 50 14420, 30, 40, 50
7Enqueue 60 10520, 30, 40, 50, 60
8Dequeue 20 20430, 40, 50, 60
9Enqueue 70 21530, 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.

Exam Tip: The logical queue order does not need to match increasing array indices. Always start displaying from Front and use circular indexing.
06

Queue Implementation Using Linked List in C

A linked-list queue stores each value inside a separate node.

Every node contains two things:

  1. Data: The value stored in the node.
  2. Next: A pointer to the next node.

Unlike an array, linked-list nodes do not need to occupy consecutive memory locations.

Figure 2: Linked List Queue Memory Diagram
Front
10 next
→
Node 2
20 next
→
Rear
30 NULL
Front points to the first node. Rear points to the last node. The final node's Next pointer is NULL.

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.

Memory Trick — DNL: Data + Next = Linked Node. Front remembers where deletion starts, Rear remembers where insertion happens.

To understand pointers and node storage in more detail, revise Linked List Representation Using Arrays and Pointers .

07

Linked List Queue Enqueue and Dequeue Algorithms

Algorithm: Enqueue Using Linked List

  1. Allocate a new node.
  2. Check whether memory allocation succeeded.
  3. Store the new value inside the node.
  4. Set its Next pointer to NULL.
  5. If the queue is empty, make Front point to the new node.
  6. Otherwise link the old Rear node to the new node.
  7. Update Rear to the new node.
Algorithm 3 Linked Enqueue
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

  1. Check whether Front is NULL.
  2. If empty, report underflow.
  3. Save the current Front node in a temporary pointer.
  4. Read its data.
  5. Move Front to the next node.
  6. If Front becomes NULL, also set Rear to NULL.
  7. Free the removed node.
Algorithm 4 Linked Dequeue
DEQUEUE():

    IF front == NULL:
        PRINT "Underflow"
        RETURN

    temp = front

    value = temp.data

    front = front.next

    IF front == NULL:
        rear = NULL

    FREE temp

    RETURN value
Important: When the last node is removed, both Front and Rear must become NULL. Otherwise Rear may point to memory that has already been freed.
08

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.

linked_queue.c C11 Program
#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

Console Output
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.

Why Is There No Fixed Array Capacity?

Nodes are allocated one at a time. The linked queue can grow until memory allocation fails or some application-defined limit is reached.

09

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.

Empty Queue Pointer State

Before Dequeue

Front → 10 → NULL

Rear → 10

After Dequeue

Front = NULL

Rear = NULL

Memory Trick — FNF: Front moves to Next, then Free the old node. If Front becomes NULL, Rear must also become NULL.
10

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 elements
Index 0 F 10
Index 1 20
Index 2 R 30
Index 3 -
Index 4 -

F = 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.

Learning Point: The simulator displays the logical operations. The complete C programs above demonstrate the actual array storage and dynamic pointer operations.
11

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.

12

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.

Exam Memory Trick: Enqueue, Dequeue and Peek = O(1). Display = O(n). Linked storage grows with active nodes; fixed array storage depends on capacity.
13

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

  1. Dequeue from an empty queue.
  2. Insert the first element.
  3. Remove the last remaining element.
  4. Attempt to insert into a full array queue.
  5. Insert after array indices wrap around.
  6. Handle memory-allocation failure.
Debugging Memory Trick — FREC: Front, Rear, Empty condition, Capacity or allocation. Check these whenever queue code fails.
14

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 .

15

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.

2 Marks

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.

2 Marks

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.

2 Marks

Q3. Why Is malloc() Used in Linked Queue?

Answer: malloc allocates memory dynamically for a new node during queue insertion.

2 Marks

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.

5 Marks

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.

5 Marks

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.

5 Marks

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.

10 Marks

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.

16

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

FIFO First In, First Out
Array Queue Uses indexed storage
Linked Queue Uses connected nodes
Front Next element to leave
Rear New insertion end
Enqueue O(1) normally
Dequeue O(1) normally
malloc() Allocate a new node
free() Release a removed node

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.

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Join Telegram Exam updates and free resources