Data Structures
Linked Traversal
Following next pointers visits linked nodes in order.
Linked Traversal
linked_traversal.c
#include <stdio.h>
struct Node {
int value;
struct Node *next;
};
int main(void) {
int last = ;
struct Node third = {last, 0};
struct Node second = {2, &third};
struct Node first = {1, &second};
struct Node *current = &first;
int sum = 0;
while (current != 0) {
sum = sum + current->value;
current = current->next;
}
printf("sum=%d\n", sum);
return 0;
}
#include <stdio.h>
struct Node {
int value;
struct Node *next;
};
int main(void) {
int last = ;
struct Node third = {last, 0};
struct Node second = {2, &third};
struct Node first = {1, &second};
struct Node *current = &first;
int sum = 0;
while (current != 0) {
sum = sum + current->value;
current = current->next;
}
printf("sum=%d\n", sum);
return 0;
}
#include <stdio.h>
struct Node {
int value;
struct Node *next;
};
int main(void) {
int last = ;
struct Node third = {last, 0};
struct Node second = {2, &third};
struct Node first = {1, &second};
struct Node *current = &first;
int sum = 0;
while (current != 0) {
sum = sum + current->value;
current = current->next;
}
printf("sum=%d\n", sum);
return 0;
}
next pointer
Each node points to the next node or to `0` at the end of the list.
traversal
A loop advances one node at a time and accumulates a result.