Dynamic Arrays and Linked Lists

What Is a Linked List in C?

A linked list is a chain of nodes, each holding a value and a pointer to the next node. See a complete C example that builds, prints and frees one.

On this page
  1. What is a linked list in C?
  2. Defining a node
  3. Adding nodes with malloc
  4. A complete program
  5. What the basic operations cost
  6. Common mistakes
  7. Linked lists in C++

What is a linked list in C?

A linked list is a chain of nodes. Each node holds a value and a pointer to the next node. The list is reached through a pointer to its first node, called the head. The last node’s next pointer is NULL, which marks the end of the list. Unlike an array, the nodes do not need to sit next to each other in memory.

Defining a node

In C, a node is usually a struct with a value and a pointer to another node of the same type:

struct Node {
    int value;
    struct Node *next;
};

In C, the struct keyword is needed each time you name the type. In C++, you can leave it out.

Adding nodes with malloc

Each node is created on the heap with malloc. This function adds a node to the front of the list and returns the new head:

struct Node *push_front(struct Node *head, int value) {
    struct Node *node = malloc(sizeof *node);
    if (node == NULL) {
        fprintf(stderr, "out of memory\n");
        exit(EXIT_FAILURE);
    }

    node->value = value;
    node->next = head;
    return node;
}

The new node points at the old head, and then becomes the head.

A complete program

This program builds a list with 10, 20 and 30, prints it, and frees every node:

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

struct Node {
    int value;
    struct Node *next;
};

struct Node *push_front(struct Node *head, int value) {
    struct Node *node = malloc(sizeof *node);
    if (node == NULL) {
        fprintf(stderr, "out of memory\n");
        exit(EXIT_FAILURE);
    }

    node->value = value;
    node->next = head;
    return node;
}

int main(void) {
    struct Node *head = NULL;   // an empty list
    head = push_front(head, 30);
    head = push_front(head, 20);
    head = push_front(head, 10);

    // Walk from the head until next is NULL
    for (struct Node *cur = head; cur != NULL; cur = cur->next) {
        printf("%d\n", cur->value);
    }

    // Free every node
    struct Node *node = head;
    while (node != NULL) {
        struct Node *next = node->next;   // save the pointer before freeing
        free(node);
        node = next;
    }

    return 0;
}

Output:

10
20
30

The for loop follows each next pointer until it reaches NULL. The while loop frees the nodes. It saves next before calling free, because the node’s memory is no longer valid after free runs.

What the basic operations cost

  • Adding to the front is quick. You create one node and change the head pointer.
  • Finding a value, or the item at position i, means walking from the head one node at a time. The cost grows with the length of the list.
  • Removing a node needs the node before it, so you can change that node’s next pointer. Finding that node usually means walking the list first.

Common mistakes

  • Losing the head. If you overwrite head without saving the old value, the rest of the list cannot be reached, and its memory leaks.
  • Dereferencing NULL. On an empty list, head is NULL, so head->value is invalid. Check for NULL before you use a node.
  • Using a node after freeing it. Once free runs, the node must not be read or changed.
  • Forgetting to free. Every malloc needs a matching free, or the memory leaks.

Linked lists in C++

C++ uses the same idea, but the standard library already has linked list containers. std::list is a doubly linked list, where each node also points back to the previous one. std::forward_list is singly linked, like the example above. More detail is in c std::list.

Nodes live on the heap, not on the stack. The difference between the two is explained in c heap vs stack. To check a program for leaked nodes, a tool such as Valgrind helps, and how to use valgrind in c shows the steps.

The complete guide to dynamic arrays and linked lists

ArrayList in C++: Using std::vector
Read the guide

Keep exploring

Guides from other topics

All topics →

The sizeof Operator in C++

Learn how the sizeof operator in C++ works: what it returns in bytes, how it handles arrays, pointers and structs, and common mistakes to avoid.

sizeof 6 min read

Bitwise XOR in C++

Learn how the bitwise XOR operator (^) in C++ works, with a truth table, code examples, and common uses such as toggling bits and swapping values.