Queue Linked List Implementation in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Queue Backed by a Linked List

A linked queue keeps head (front) and tail (back) pointers. Enqueue links a node after tail; dequeue unlinks the head. Both are O(1), there is no capacity limit, and no wasted slots. When the last node is dequeued, set tail to NULL too.

Remember
struct Queue { struct Node *head, *tail; };
enqueue: tail ? (tail->next = n) : (head = n); tail = n;
dequeue: head = head->next; if (!head) tail = NULL;

Cheat Sheet

Keep these facts handy while you work with Queue Linked List Implementation in C in C.

TopicDetail
Fronthead
Backtail
EnqueueLink after tail
GotchaReset tail when empty

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Circular Queue.

Example 1 — A linked queue

Enqueue three values and drain the queue.

C
#include <stdio.h>
#include <stdlib.h>

struct Node { int data; struct Node *next; };
struct Queue { struct Node *head, *tail; };

static void enqueue(struct Queue *q, int v) {
    struct Node *n = malloc(sizeof *n);
    if (!n) exit(1);
    n->data = v;
    n->next = NULL;
    if (q->tail) q->tail->next = n;
    else q->head = n;
    q->tail = n;
}
static int dequeue(struct Queue *q) {
    struct Node *n = q->head;
    int v = n->data;
    q->head = n->next;
    if (!q->head) q->tail = NULL;
    free(n);
    return v;
}

int main(void) {
    struct Queue q = {NULL, NULL};
    enqueue(&q, 10); enqueue(&q, 20); enqueue(&q, 30);

    printf("dequeued:");
    while (q.head) printf(" %d", dequeue(&q));
    printf("\nempty: %d\n", q.head == NULL && q.tail == NULL);
    return 0;
}

How It Works

  1. The tail pointer makes enqueue O(1).
  2. Dequeue frees the old head node.
  3. When the last node leaves, tail is set to NULL.

Edge Cases & Pitfalls

Check these before relying on Queue Linked List Implementation in C in real programs.

tail

Dangling tail

If tail still points at a freed node after the last dequeue, the next enqueue writes to freed memory.

empty

Dequeue when empty

Check q->head first.

Key Takeaways

  • Head front, tail back.
  • Reset tail when empty.
  • O(1) both ends.

One line: A linked queue keeps head (front) and tail (back) pointers.

Frequently Asked Questions

It lets you append in O(1) instead of walking the whole list.
Set both head and tail to NULL so the queue is truly empty.
Continue with "C Circular Queue" next. Wrap indexes with modulo so no space is wasted. The C Queues hub lists every topic in order.

Next: Circular Queue

Wrap indexes with modulo so no space is wasted.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful