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
nextpointer. Finding that node usually means walking the list first.
Common mistakes
- Losing the head. If you overwrite
headwithout saving the old value, the rest of the list cannot be reached, and its memory leaks. - Dereferencing NULL. On an empty list,
headisNULL, sohead->valueis invalid. Check forNULLbefore you use a node. - Using a node after freeing it. Once
freeruns, the node must not be read or changed. - Forgetting to free. Every
mallocneeds a matchingfree, 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.