Linked lists in K&R C
last modified December 5, 2026 K&R C 1978
A linked list is a chain of structures, the nodes, in which each node points to the next one. Unlike an array, a list grows one node at a time, and a node can be inserted or removed by changing a pointer or two, without moving the others. The structures article linked a few external nodes by hand. This article creates the nodes while the program runs, and builds, searches, changes, and frees lists with them.
A node from malloc
A program that does not know in advance how many nodes it needs gets
their storage from the library. malloc(n) returns a pointer
to n bytes of storage, or NULL when there is no
more memory. Unlike calloc, it does not clear the storage.
free gives the storage back.
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
int main()
{
struct node *p;
p = (struct node *) malloc(sizeof(struct node));
if (p == NULL) {
printf("out of memory\n");
return 1;
}
p->value = 42;
p->next = NULL;
printf("value %d, last node %s\n", p->value,
p->next == NULL ? "yes" : "no");
printf("a node takes %d bytes\n", (int) sizeof(struct node));
free((char *) p);
return 0;
}
malloc returns a char pointer, which the cast
converts to a pointer to struct node. The size of the node
comes from sizeof, never from counting the bytes of the
members by hand. The program checks for NULL before it uses
the node. The members get their values through the pointer; the null
pointer in next marks the end of the list, here a list of
one node.
$ gcc -std=c89 -pedantic -Wall -Wextra -o onenode onenode.c $ ./onenode value 42, last node yes a node takes 16 bytes
Adding at the front
A list is reached through a pointer to its first node, the
head. The empty list is a head that is NULL. The
cheapest place to add a node is the front: the new node points to the
old first node and becomes the head.
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
/* a new node holding value, linked to next */
struct node *newnode(value, next)
int value;
struct node *next;
{
struct node *p;
p = (struct node *) malloc(sizeof(struct node));
if (p == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
p->value = value;
p->next = next;
return p;
}
int printlist(p)
struct node *p;
{
for (; p != NULL; p = p->next)
printf("%d ", p->value);
printf("\n");
return 0;
}
int main()
{
struct node *head;
int i;
head = NULL; /* the empty list */
for (i = 1; i <= 4; i++) {
head = newnode(i * 10, head);
printlist(head);
}
return 0;
}
newnode allocates a node, fills in both members, and
returns it. It returns a pointer, so its definition starts with the type
struct node *; it is defined before main, which
therefore knows the type. If malloc fails, the program
cannot go on, and newnode ends it with exit.
head = newnode(i * 10, head) links the new node to the old
list and makes it the new head. printlist follows the
next pointers until it finds NULL. Nodes added
at the front come out in the reverse order.
$ gcc -std=c89 -pedantic -Wall -Wextra -o front front.c $ ./front 10 20 10 30 20 10 40 30 20 10
Adding at the end
To keep the nodes in the order in which they come, each new node must go to the end of the list. Walking the list to find the end each time would take longer and longer, so the program keeps a second pointer, the tail, to the last node.
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
struct node *newnode(value, next)
int value;
struct node *next;
{
struct node *p;
p = (struct node *) malloc(sizeof(struct node));
if (p == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
p->value = value;
p->next = next;
return p;
}
int printlist(p)
struct node *p;
{
for (; p != NULL; p = p->next)
printf("%d ", p->value);
printf("\n");
return 0;
}
/* read numbers and keep them in the order of the input */
int main()
{
struct node *head, *tail, *p;
int n;
head = tail = NULL;
while (scanf("%d", &n) == 1) {
p = newnode(n, NULL);
if (head == NULL)
head = p; /* the first node */
else
tail->next = p; /* after the last node */
tail = p;
}
printlist(head);
return 0;
}
The program reads numbers until scanf stops finding them.
Each new node has no successor. The first one becomes the head, and every
later one is linked to the next member of the current last
node. In both cases the new node becomes the tail. The order of the input
is kept.
$ gcc -std=c89 -pedantic -Wall -Wextra -o tail tail.c $ echo 30 10 50 20 40 | ./tail 30 10 50 20 40
A sorted list
A list can also be kept in order. Each new value is inserted in front of the first node with a larger value, so the list is sorted at every moment and never needs a sort.
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
struct node *newnode(value, next)
int value;
struct node *next;
{
struct node *p;
p = (struct node *) malloc(sizeof(struct node));
if (p == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
p->value = value;
p->next = next;
return p;
}
int printlist(p)
struct node *p;
{
for (; p != NULL; p = p->next)
printf("%d ", p->value);
printf("\n");
return 0;
}
/* insert value in order; return the new head */
struct node *insorted(head, value)
struct node *head;
int value;
{
struct node *p, *prev;
prev = NULL;
for (p = head; p != NULL && p->value < value; p = p->next)
prev = p;
if (prev == NULL)
return newnode(value, head); /* a new first node */
prev->next = newnode(value, p);
return head;
}
int main()
{
struct node *head;
int n;
head = NULL;
while (scanf("%d", &n) == 1) {
head = insorted(head, n);
printlist(head);
}
return 0;
}
insorted walks the list until it finds a node whose value is
not smaller than the new one, remembering the node before it in
prev. A single pointer would not do: the new node must be
linked into the next member of the node before. If
prev is still NULL, the new value belongs at
the front, and the new node becomes the head, which is why the function
returns the head. The program prints the list after each insertion.
$ gcc -std=c89 -pedantic -Wall -Wextra -o sorted sorted.c $ echo 30 10 50 20 40 | ./sorted 30 10 30 10 30 50 10 20 30 50 10 20 30 40 50
Freeing a list
The nodes stay allocated until the program ends or frees them. A program that builds lists over and over must free each list when it is done with it.
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
struct node *newnode(value, next)
int value;
struct node *next;
{
struct node *p;
p = (struct node *) malloc(sizeof(struct node));
if (p == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
p->value = value;
p->next = next;
return p;
}
/* free every node of the list; return their number */
int freelist(p)
struct node *p;
{
struct node *next;
int n;
for (n = 0; p != NULL; p = next) {
next = p->next; /* read it before p is freed */
free((char *) p);
n++;
}
return n;
}
int main()
{
struct node *head;
int i;
head = NULL;
for (i = 1; i <= 1000; i++)
head = newnode(i, head);
printf("%d nodes freed\n", freelist(head));
head = NULL;
return 0;
}
freelist frees the nodes one by one. It reads the
next member before it frees the node: after
free, the node belongs to the allocator, which may already
have written over it. The tempting loop for (; p != NULL; p =
p->next) free(p); reads p->next from a freed
node. It may even seem to work, because the freed storage often still
holds the old values, but it is an error. After the call,
head still points to the freed first node, so the program
sets it to NULL.
$ gcc -std=c89 -pedantic -Wall -Wextra -o freeing freeing.c $ ./freeing 1000 nodes freed
A list module
The same functions appear in every program of this article. They move to
a file of their own, list.c, with a header that declares the
node and the functions, as in the
program structure article. Later
programs show only their new function and main.
struct node {
int value;
struct node *next;
};
struct node *newnode(), *insorted();
int printlist(), freelist();
The header declares the structure and the functions that return
pointers; printlist and freelist return
int and are declared too, so that no call depends on an
implicit declaration.
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
struct node *newnode(value, next)
int value;
struct node *next;
{
struct node *p;
p = (struct node *) malloc(sizeof(struct node));
if (p == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
p->value = value;
p->next = next;
return p;
}
struct node *insorted(head, value)
struct node *head;
int value;
{
struct node *p, *prev;
prev = NULL;
for (p = head; p != NULL && p->value < value; p = p->next)
prev = p;
if (prev == NULL)
return newnode(value, head);
prev->next = newnode(value, p);
return head;
}
int printlist(p)
struct node *p;
{
for (; p != NULL; p = p->next)
printf("%d ", p->value);
printf("\n");
return 0;
}
int freelist(p)
struct node *p;
{
struct node *next;
int n;
for (n = 0; p != NULL; p = next) {
next = p->next;
free((char *) p);
n++;
}
return n;
}
The file holds newnode, insorted,
printlist, and freelist from the previous
sections.
Deleting a node
Deleting a node is the reverse of inserting one: the node before it must point to the node after it, and the removed node is freed.
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
/* remove the first node holding value; return the new head */
struct node *delvalue(head, value)
struct node *head;
int value;
{
struct node *p, *prev;
prev = NULL;
for (p = head; p != NULL && p->value != value; p = p->next)
prev = p;
if (p == NULL)
return head; /* not in the list */
if (prev == NULL)
head = p->next; /* removing the first node */
else
prev->next = p->next;
free((char *) p);
return head;
}
int main()
{
static int gone[] = { 30, 10, 50, 99 };
struct node *head;
int i;
head = NULL;
for (i = 10; i <= 50; i += 10)
head = insorted(head, i);
printlist(head);
for (i = 0; i < 4; i++) {
head = delvalue(head, gone[i]);
printf("without %d: ", gone[i]);
printlist(head);
}
freelist(head);
return 0;
}
delvalue looks for the first node with the value, keeping
the node before it in prev. A value that is not in the list
leaves the list unchanged. When the first node is removed, the head moves
to the second node; otherwise the node before skips over the removed one.
The node is freed only after it is unlinked. The program deletes from the
middle, the front, and the end, and then a value that is not there.
$ gcc -std=c89 -pedantic -Wall -Wextra -o delnode delnode.c list.c $ ./delnode 10 20 30 40 50 without 30: 10 20 40 50 without 10: 20 40 50 without 50: 20 40 without 99: 20 40
Reversing a list
A list can be reversed without allocating a node or moving a value: it is enough to turn every link around.
#include <stdio.h>
#include "list.h"
/* reverse the links of a list; return the new head */
struct node *reverse(head)
struct node *head;
{
struct node *prev, *next;
prev = NULL;
while (head != NULL) {
next = head->next;
head->next = prev; /* turn the link around */
prev = head;
head = next;
}
return prev;
}
int main()
{
struct node *head;
int i;
head = NULL;
for (i = 1; i <= 6; i++)
head = insorted(head, i * i);
printlist(head);
head = reverse(head);
printlist(head);
printf("%d nodes freed\n", freelist(head));
return 0;
}
reverse takes the nodes off the front of the list one by
one and puts each in front of a second list, which starts empty in
prev. Before a link is changed, the rest of the list is
saved in next. When the original list is used up,
prev is the head of the reversed list. The program builds a
sorted list of squares, reverses it, and frees it.
$ gcc -std=c89 -pedantic -Wall -Wextra -o reverse reverse.c list.c $ ./reverse 1 4 9 16 25 36 36 25 16 9 4 1 6 nodes freed
Running out of memory
Each node costs memory, and memory runs out. Every program in this
article checks the value of malloc; the next one shows what
the check is for.
#include <stdio.h>
#include <stdlib.h>
#define WANTED 20000
struct node {
int value;
struct node *next;
};
int main()
{
struct node *head, *p;
int n;
head = NULL;
for (n = 0; n < WANTED; n++) {
p = (struct node *) malloc(sizeof(struct node));
if (p == NULL) {
printf("out of memory after %d nodes\n", n);
break;
}
p->value = n;
p->next = head;
head = p;
}
printf("list of %d nodes of %d bytes\n", n, (int) sizeof(struct node));
for (n = 0; head != NULL; n++) {
p = head->next;
free((char *) head);
head = p;
}
printf("%d nodes freed\n", n);
return 0;
}
The program tries to build a list of 20000 nodes and stops when
malloc returns NULL. Then it frees what it has.
On a modern machine, 20000 nodes of 16 bytes are no problem.
$ gcc -std=c89 -pedantic -Wall -Wextra -o many many.c $ ./many list of 20000 nodes of 16 bytes 20000 nodes freed
On the PDP-11, a program has 64 kilobytes for its code, its data, and
its stack together. A node takes only 4 bytes there, and
malloc adds 2 bytes of its own to each block, but the
memory is gone after 8874 nodes. Without the check, the program would
store through a null pointer.
$ cc many.c $ a.out out of memory after 8874 nodes list of 8874 nodes of 4 bytes 8874 nodes freed
Source
Dennis Ritchie: C Reference Manual (1975)
Linux manual: malloc, free, calloc
In this article we have worked with linked lists in K&R C: nodes
from malloc, adding at the front and at the end, a sorted
list, freeing a list correctly, a list module, deleting and reversing,
and a program that runs out of memory on the PDP-11. The next article
builds binary trees, and the
storage allocator article shows how
malloc and free work.
Author
List all K&R C tutorials.