C Doubly Linked Lists

Intermediate
6 min read
Updated: Nov 2026
1 example

Two Pointers per Node

A doubly linked list node has both prev and next pointers. You can traverse in either direction and delete a node in O(1) when you have a pointer to it. The cost is an extra pointer per node and more links to keep consistent on every insert and delete. A tail pointer enables fast appends.

Remember
struct DNode {
    int data;
    struct DNode *prev, *next;
};
struct DList { struct DNode *head, *tail; };

Cheat Sheet

Keep these facts handy while you work with Doubly Linked Lists in C.

TopicDetail
Linksprev and next
Delete known nodeO(1)
MemoryExtra pointer per node
TraverseForward or backward

C Examples

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

Example 1 — Append and walk both directions

Keep head and tail in a small struct.

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

struct DNode { int data; struct DNode *prev, *next; };
struct DList { struct DNode *head, *tail; };

static void push_back(struct DList *l, int v) {
    struct DNode *n = malloc(sizeof *n);
    if (!n) exit(1);
    n->data = v;
    n->next = NULL;
    n->prev = l->tail;
    if (l->tail) l->tail->next = n;
    else l->head = n;
    l->tail = n;
}

int main(void) {
    struct DList list = {NULL, NULL};
    for (int i = 1; i <= 3; i++) push_back(&list, i);

    printf("forward:");
    for (struct DNode *p = list.head; p; p = p->next) printf(" %d", p->data);
    printf("\nbackward:");
    for (struct DNode *p = list.tail; p; p = p->prev) printf(" %d", p->data);
    printf("\n");

    while (list.head) {
        struct DNode *t = list.head->next;
        free(list.head);
        list.head = t;
    }
    return 0;
}

How It Works

  1. Each new node points back to the old tail with prev.
  2. The old tail points forward to the new node with next.
  3. Walking prev from the tail gives the reverse order.

Edge Cases & Pitfalls

Check these before relying on Doubly Linked Lists in real programs.

four

Updating only one direction

Every insert or delete must fix up both prev and next of the neighbors.

null

Neighbors that are NULL

Check for a missing prev or next at the ends before dereferencing.

Key Takeaways

  • Two-way links.
  • O(1) delete with a node pointer.
  • Keep both directions consistent.

One line: A doubly linked list node has both prev and next pointers.

Frequently Asked Questions

You can traverse in both directions and delete a given node in O(1) without searching for its predecessor.
Each node needs an extra pointer, and every insert and delete must update more links.
Continue with "C Circular Linked Lists" next. The last node links back to the first for endless looping. The C Linked Lists hub lists every topic in order.

Next: Circular Linked Lists

The last node links back to the first for endless looping.

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