📘 Lesson  ·  Lesson 64

Queue using Linked List

Why a linked-list queue?

A queue follows FIFO — First In, First Out. You can build one with an array (see queue in C), but a simple array queue has two weaknesses: a fixed size, and wasted slots after several dequeues. A linked-list queue fixes both by allocating a node for each item and freeing it when removed.

If linked lists are new, read linked list in C first — this lesson builds directly on nodes.

front and rear pointers

We track the queue with two pointers into the node chain:

PointerPoints to
frontThe first node — next to be removed
rearThe last node — where we add

Both start as NULL for an empty queue.

Enqueue at the rear

Create a node, attach it after rear, and move rear forward.

C Language
void enqueue(int x) {
    struct Node *n = malloc(sizeof(struct Node));
    n->data = x; n->next = NULL;
    if (rear == NULL) { front = rear = n; return; }  // empty
    rear->next = n;       // link after current rear
    rear = n;            // new rear
}

Dequeue at the front

Take the front node, advance front, and free the old node.

C Language
int dequeue() {
    if (front == NULL) { printf("Queue empty\n"); return -1; }
    struct Node *temp = front;
    int x = temp->data;
    front = front->next;
    if (front == NULL) rear = NULL;   // was the last node!
    free(temp);
    return x;
}
⚠️ The last-node trap

When you dequeue the final node, front becomes NULL. You must also reset rear to NULL, or it will dangle at freed memory.

The complete program

C Language
#include <stdio.h>
#include <stdlib.h>
struct Node { int data; struct Node *next; };
struct Node *front = NULL, *rear = NULL;

void enqueue(int x) {
    struct Node *n = malloc(sizeof(struct Node));
    n->data = x; n->next = NULL;
    if (rear == NULL) { front = rear = n; return; }
    rear->next = n; rear = n;
}
int dequeue() {
    if (front == NULL) { printf("Empty\n"); return -1; }
    struct Node *temp = front;
    int x = temp->data;
    front = front->next;
    if (front == NULL) rear = NULL;
    free(temp);
    return x;
}
int main() {
    enqueue(10); enqueue(20); enqueue(30);
    printf("Removed: %d\n", dequeue());  // 10
    printf("Removed: %d\n", dequeue());  // 20
    return 0;
}
Output:
Removed: 10
Removed: 20

Dry run

ActionfrontrearQueue
enqueue 10101010
enqueue 20102010, 20
enqueue 30103010, 20, 30
dequeue → 10203020, 30

Common mistakes

  • Forgetting to set rear = NULL when the last node is dequeued.
  • Not handling the empty case in enqueue (both pointers start NULL).
  • Forgetting to free the dequeued node, causing a memory leak.
  • Adding at the front or removing at the rear — that breaks FIFO order.
🏋️ Practice

Add a display() function that walks from front to the end printing each value, and call it after every operation to watch the queue change.

Summary

  • A linked-list queue uses front and rear node pointers.
  • Enqueue adds a node at the rear; dequeue removes from the front.
  • It grows and shrinks at run time — no fixed size, no wasted slots.
  • Reset rear to NULL when the last node leaves.
  • Always free the removed node to avoid leaks.

Frequently Asked Questions

How do you implement a queue using a linked list in C?
You keep two pointers, front and rear. To enqueue, you create a new node and attach it after rear, then move rear to it. To dequeue, you take the node at front, move front to the next node, and free the old one. This gives a queue that grows and shrinks at run time.
Why use a linked list instead of an array for a queue?
A linked-list queue has no fixed size, so it never overflows until memory runs out, and it never wastes slots the way a simple array queue does after several dequeues. Each node is allocated only when needed and freed when removed, so space matches the actual number of items.
What do front and rear point to in a linked-list queue?
front points to the first node — the next item to be removed — and rear points to the last node, where new items are added. When the queue is empty, both are NULL.
What happens when you dequeue the last element?
After removing the only node, front becomes NULL. You must also set rear to NULL, otherwise rear would point to freed memory and the next enqueue would misbehave. Handling this case is a common source of bugs.
Is a linked-list queue FIFO?
Yes. Items are added at the rear and removed from the front, so the first item enqueued is the first one dequeued — exactly the First In, First Out behaviour of any queue, just implemented with nodes instead of an array.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.