Traversal and Search
Walk the list.
Traversal and Search is a free C Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Walking the list
Traversal means visiting each node in order. You start at the head and follow next pointers until you reach NULL.
Almost every list algorithm builds on this simple walk.
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int main(void) {
struct Node *head = make(10);
head->next = make(20);
for (struct Node *p = head; p != NULL; p = p->next)
printf("%d ", p->value);
printf("\n");
return 0;
}The traversal pattern
The canonical loop uses a moving pointer p: initialize it to head, continue while p is not NULL, and advance with p = p->next.
Never modify head itself while walking, or you lose the start of the list.
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int main(void) {
struct Node *head = make(1);
head->next = make(2);
struct Node *p = head;
while (p) { printf("%d ", p->value); p = p->next; }
printf("\n");
return 0;
}Counting nodes
To find the length, walk the list and increment a counter for each node.
This is an O(n) operation because the count is not stored anywhere.
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int length(struct Node *head) {
int n = 0;
for (struct Node *p = head; p; p = p->next) n++;
return n;
}
int main(void) {
struct Node *head = make(1);
head->next = make(2);
head->next->next = make(3);
printf("length = %d\n", length(head));
return 0;
}Summing values
Traversal lets you aggregate data. Here we add up all the integer values in the list.
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int main(void) {
struct Node *head = make(5);
head->next = make(10);
int sum = 0;
for (struct Node *p = head; p; p = p->next) sum += p->value;
printf("sum = %d\n", sum);
return 0;
}Searching for a value
To find a value, walk the list and compare each node. Return the node (or its position) when you find a match, or signal failure if you reach the end.
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
struct Node *find(struct Node *head, int v) {
for (struct Node *p = head; p; p = p->next)
if (p->value == v) return p;
return NULL;
}
int main(void) {
struct Node *head = make(1);
head->next = make(2);
printf("found 2: %d\n", find(head, 2) != NULL);
printf("found 9: %d\n", find(head, 9) != NULL);
return 0;
}Finding a position
Sometimes you want the index of a match rather than the node. Keep a counter as you walk and return it when the value is found.
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int index_of(struct Node *head, int v) {
int i = 0;
for (struct Node *p = head; p; p = p->next, i++)
if (p->value == v) return i;
return -1;
}
int main(void) {
struct Node *head = make(7);
head->next = make(8);
printf("%d\n", index_of(head, 8));
return 0;
}Accessing the nth node
Linked lists have no direct indexing. To reach position n, you must step n times from the head.
This is why random access is O(n) compared to an array's O(1).
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
struct Node *at(struct Node *head, int n) {
struct Node *p = head;
for (int i = 0; i < n && p; i++) p = p->next;
return p;
}
int main(void) {
struct Node *head = make(10);
head->next = make(20);
head->next->next = make(30);
printf("%d\n", at(head, 2)->value);
return 0;
}Finding the last node
To get the tail, walk until p->next is NULL. That node is the last one.
Be careful with an empty list, where head itself is NULL.
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int main(void) {
struct Node *head = make(1);
head->next = make(2);
head->next->next = make(3);
struct Node *p = head;
while (p->next) p = p->next;
printf("last = %d\n", p->value);
return 0;
}Finding the maximum
Combining search and aggregation, you can find the largest value by tracking a running best during traversal.
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int main(void) {
struct Node *head = make(3);
head->next = make(9);
head->next->next = make(5);
int best = head->value;
for (struct Node *p = head->next; p; p = p->next)
if (p->value > best) best = p->value;
printf("max = %d\n", best);
return 0;
}Recursive traversal
Lists can also be walked recursively: process the current node, then recurse on next.
This is elegant but uses stack space proportional to the length, so iteration is safer for very long lists.
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
void print_rec(struct Node *p) {
if (!p) { printf("\n"); return; }
printf("%d ", p->value);
print_rec(p->next);
}
int main(void) {
struct Node *head = make(1);
head->next = make(2);
print_rec(head);
return 0;
}Guard against empty lists
Every traversal function should handle an empty list (head == NULL) gracefully.
The standard loop already does: the condition p != NULL is false immediately, so the body never runs.
#include <stdio.h>
struct Node { int value; struct Node *next; };
int length(struct Node *head) {
int n = 0;
for (struct Node *p = head; p; p = p->next) n++;
return n;
}
int main(void) {
struct Node *head = NULL;
printf("empty length = %d\n", length(head));
return 0;
}Quick Check
Test your understanding of list traversal cost.
Recap
You learned to traverse and search lists:
- The walk pattern: start at
head, loop while notNULL, advance withp = p->next. - Counting, summing, and finding max are all built on traversal.
- Search compares each node; index access is O(n).
- Traversal can be recursive, but iteration is safer for long lists; always handle the empty case.
Frequently asked questions
Is the “Traversal and Search” lesson free?
Yes — the full text of “Traversal and Search” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Traversal and Search”?
Walk the list. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Traversal and Search” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Singly Linked Lists
- Insertion and Deletion
- Traversal and Search
- Doubly Linked Lists