Home C Queues Queue Linked List Implementation in C Queue Linked List Implementation in C Definition
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.
struct Queue { struct Node *head, *tail; };
enqueue: tail ? (tail->next = n) : (head = n); tail = n;
dequeue: head = head->next; if (!head) tail = NULL; Reference
Cheat Sheet Keep these facts handy while you work with Queue Linked List Implementation in C in C.
Topic Detail Front head Back tail Enqueue Link after tail Gotcha Reset tail when empty
Code
C Examples Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Circular Queue .
1. A linked queue Example 1 — A linked queue Enqueue three values and drain the queue.
#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;
} dequeued: 10 20 30
empty: 1 How It Works The tail pointer makes enqueue O(1). Dequeue frees the old head node. 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.
Remember
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 Why use a tail pointer in a linked queue? It lets you append in O(1) instead of walking the whole list.
What must you do when the last element is dequeued? Set both head and tail to NULL so the queue is truly empty.
What should I learn after Queue Linked List Implementation in C? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion