An array list must shift elements and has a fixed size. A linked list avoids both problems by storing each element in its own block of memory and connecting the blocks with pointers. This page introduces the node, the head pointer and the basic loop that walks along a list.
Nodes and links
Intuition
A train is a chain of coaches. Each coach is coupled to the next one. If you know where the engine is, you can walk through the whole train coach by coach; the last coach has nothing coupled behind it. The coaches do not need to be built next to each other in the factory, only coupled in the right order.
Definition 3.5 · Singly linked list
A singly linked list is a sequence of nodes in which each node holds a data item and a pointer (link ) to the next node. A separate pointer, the head , holds the address of the first node; the last node’s link is NULL. The empty list is head == NULL.
Node type Copy typedef struct node {
int data; /* the element */
struct node *next; /* address of the next node, or NULL at the end */
} Node;
Node *head = NULL; /* an empty list */
A list with three nodes (addresses are examples) Copy head 1000 1050 1200
+------+ +-------+------+ +-------+------+ +------+------+
| 1000 | ---> | madan | 1050 | --> | mohan | 1200 | --> | babu | NULL |
+------+ +-------+------+ +-------+------+ +------+------+
The nodes can be anywhere in memory; only the links fix their order.
Creating nodes and traversing
A new node is created at run time with malloc, which may return NULL when memory runs out, so the result must be checked. Every node created with malloc must be released with free when it is no longer needed.
To traverse the list (visit every node), start at head and follow next until you reach NULL:
The traversal loop Copy for (Node *p = head; p != NULL; p = p->next)
visit(p->data);
The same loop counts the nodes (Check Your Progress, Q4) or prints their addresses (Q1): replace visit by count++ or by printf("%p\n", (void *)p). Each is O ( n ) O(n) O ( n ) .
Example 3.6 Build, traverse, count and free a list
Goal. Start from the empty list, append 10, 20 and 30, print the list, count its nodes two ways and free it.
Program. append receives the address of head (Node **), because adding the first node must change head itself.
C · sll_basic.c Copy #include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node * next; /* address of the next node, or NULL */
} Node;
Node * new_node( int x)
{
Node * n = malloc( sizeof * n);
if (n == NULL ) {
fprintf(stderr, "out of memory \n " );
exit(EXIT_FAILURE);
}
n->data = x;
n->next = NULL ;
return n;
}
/* Add x at the end. head is passed by address because it may change. */
void append(Node ** head, int x)
{
Node * n = new_node(x);
if ( * head == NULL ) { /* empty list: n becomes the first node */
* head = n;
return ;
}
Node * p = * head;
while (p->next != NULL ) /* walk to the last node */
p = p->next;
p->next = n;
}
void traverse( const Node * head)
{
for ( const Node * p = head; p != NULL ; p = p->next)
printf( "%d -> " , p->data);
printf( "NULL \n " );
}
int count( const Node * head) /* iterative */
{
int c = 0 ;
for ( const Node * p = head; p != NULL ; p = p->next)
c ++ ;
return c;
}
int count_rec( const Node * head) /* recursive: empty list has 0 nodes */
{
return head == NULL ? 0 : 1 + count_rec(head->next);
}
void free_list(Node * head)
{
while (head != NULL ) {
Node * next = head->next; /* remember the rest before freeing */
free(head);
head = next;
}
}
int main( void )
{
Node * head = NULL ; /* the empty list */
printf( "empty list: " );
traverse(head);
printf( "count = %d \n " , count(head));
append( & head, 10 );
append( & head, 20 );
append( & head, 30 );
traverse(head);
printf( "count = %d, count_rec = %d \n " , count(head), count_rec(head));
free_list(head);
return 0 ;
}
Run it.
Output Copy empty list: NULL
count = 0
10 -> 20 -> 30 -> NULL
count = 3, count_rec = 3
Result. The list 10 → 20 → 30 → NULL has 3 nodes by both the loop and the recursive count; the empty list correctly has 0.
Correction · Program 3.4 (creating a linked list)
Study material says: Program 3.4 (§3.4) writes #define NULL 0 after including <stdlib.h>, uses void main(), always mallocs a first node, and ends input with −1111, which is stored in an extra last node; traverse and count skip that node by stopping when next == NULL.
Correct version: Do not redefine NULL (the standard headers define it). Use int main(void). Represent the empty list as head == NULL, create a node only for real data, check every malloc result, and stop traversal when the pointer is NULL (p != NULL), not when p->next is NULL. Free the nodes at the end.
Why: Redefining a standard macro is not allowed (C17 §7.1.3) and compilers warn about it. With the extra −1111 node, an “empty” list still contains one node, and every function must remember to skip the last node; a function that does not (as in Program 3.8 later) prints −1111 as data.
Correction · Answer to Check Your Progress, question 4 (count items)
Study material says: §3.9 counts the items with while(temp->next != NULL) { count++; } and then adds 1.
Correct version: advance the pointer in every iteration and test the pointer itself, so the empty list gives 0:
Counting the nodes correctly Copy int count_items(const Node *head)
{
int count = 0;
for (const Node *p = head; p != NULL; p = p->next)
count++;
return count;
} Why: The study-material loop never changes temp, so for any list with two or more nodes it never ends (a test run was still looping after 1,000,000 iterations). It also dereferences head before checking it, so an empty list crashes. The same answer for Q1 prints addresses with %u; the correct conversion for a pointer is %p with a (void *) cast (C17 §7.21.6.1).
Key idea
A singly linked list is reached through head; every algorithm walks it with for (p = head; p != NULL; p = p->next), and the empty list is simply head == NULL.
TEE Exam note
For “what is a linked list? Write a function to count/print its nodes”: draw boxes with data and link fields, arrows between them, head pointing to the first and NULL in the last. Give the node struct, then the traversal loop with p != NULL. Mention that malloc must be checked and that the empty list is head == NULL.
Common mistake
Testing p->next != NULL instead of p != NULL in a traversal loop. That skips the last node and crashes on an empty list.
Common mistake
Freeing a node and then reading its next field (free(p); p = p->next;). Save p->next in a variable before calling free.
Key takeaways
A node = data + pointer to the next node; the list is reached through head; the last link is NULL.
Empty list: head == NULL; nodes are created with malloc (checked) and released with free.
Traversal, counting and printing all use the loop p = head; p != NULL; p = p->next: O ( n ) O(n) O ( n ) .
A function that may change head must receive &head or return the new head.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice How is an empty singly linked list represented?
A A node whose data is 0 B A node whose next is NULL C A head pointer equal to NULL D An array of size 0
Check answer
Show answer Answer: C. A head pointer equal to NULL
With no nodes there is nothing for head to point to. A node whose next is NULL is a list with one element, not an empty list.
Question 2 · multiple choice What does this program print?
C · q3-traverse.c Copy #include <stdio.h>
typedef struct node {
int data;
struct node * next;
} Node;
int main( void )
{
Node c = { 30 , NULL }, b = { 20 , & c}, a = { 10 , & b};
int sum = 0 , n = 0 ;
for (Node * p = & a; p->next != NULL ; p = p->next) {
sum += p->data;
n ++ ;
}
printf( "%d %d \n " , n, sum);
return 0 ;
} A 3 60 B 2 30 C 3 30 D 2 60
Check answer
Show answer Answer: B. 2 30
The condition p->next != NULL stops at the last node without visiting it, so only 10 and 20 are added. With p != NULL the result would be 3 60.
Question 3 · multiple choice Why does append in the example take a parameter of type Node **?
A Because a node contains two pointers B Because appending to an empty list must change the caller’s head variable C Because malloc returns a pointer to a pointer D Because C cannot pass structures to functions
Check answer
Show answer Answer: B. Because appending to an empty list must change the caller’s head variable
C passes arguments by value. To change the caller’s head, the function needs its address. The alternative is to return the new head.
Question 4 · multiple choice What is the time complexity of counting the nodes of a singly linked list with n n n nodes?
Check answer
Show answer Answer: D. O ( n ) O(n) O ( n )
Each node is visited once. Counting is O ( 1 ) O(1) O ( 1 ) only if the list keeps a separate size field that is updated on every insertion and deletion.
Question 5 · multiple choice Which line correctly frees every node of a list?
A while (head) { free(head); head = head->next; }B free(head);C while (head) { Node *n = head->next; free(head); head = n; }D for (Node *p = head; p; p = p->next) free(p);
Check answer
Show answer Answer: C. while (head) { Node *n = head->next; free(head); head = n; }
The next pointer must be saved before the node is freed. The first and last options read next from memory that has already been freed; free(head) alone frees only the first node.
Question 6 · short answer
Write a C function that prints the memory address and data of every node of a singly linked list (Check Your Progress, Q1).
Show worked answer void print_locations(const Node *head)
{
for (const Node *p = head; p != NULL; p = p->next)
printf("%p: %d\n", (void *)p, p->data);
}
The loop works for an empty list (prints nothing), and %p with a (void *) cast is the correct way to print a pointer. The addresses differ from run to run.