Bird
0
0
DSA Cprogramming

Traversal and Printing a Linked List in DSA C

Choose your learning style9 modes available
Mental Model
We start at the first item and move step-by-step to the next until we reach the end.
Analogy: Like reading a chain of paper clips one by one from the first to the last without skipping any.
head -> [1] -> [2] -> [3] -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, print all values
Goal: Print all values in the list from start to end in order
Step 1: Start at the first node with value 1
head -> [1 ↑] -> [2] -> [3] -> null
Why: We begin printing from the first node
Step 2: Print value 1 and move to next node
head -> [1] -> [2 ↑] -> [3] -> null
Why: After printing current, move to next to continue
Step 3: Print value 2 and move to next node
head -> [1] -> [2] -> [3 ↑] -> null
Why: Continue printing each node in order
Step 4: Print value 3 and move to next node
head -> [1] -> [2] -> [3] -> null ↑
Why: Print last node and reach end (null)
Result:
head -> [1] -> [2] -> [3] -> null
Printed output: 1 2 3 
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Define node structure
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// Function to create a new node
Node* createNode(int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

// Function to print the linked list
void printList(Node* head) {
    Node* current = head; // Start at head
    while (current != NULL) { // Traverse until end
        printf("%d ", current->data); // Print current node's data
        current = current->next; // Move to next node
    }
    printf("\n");
}

int main() {
    // Create linked list 1 -> 2 -> 3 -> null
    Node* head = createNode(1);
    head->next = createNode(2);
    head->next->next = createNode(3);

    // Print the list
    printList(head);

    // Free memory
    Node* temp;
    while (head != NULL) {
        temp = head;
        head = head->next;
        free(temp);
    }

    return 0;
}
Node* current = head; // Start at head
Initialize pointer to start traversal from first node
while (current != NULL) { // Traverse until end
Continue until we reach the end of the list
printf("%d ", current->data); // Print current node's data
Output the value stored in current node
current = current->next; // Move to next node
Advance pointer to next node to continue traversal
OutputSuccess
1 2 3
Complexity Analysis
Time: O(n) because we visit each of the n nodes exactly once
Space: O(1) because we only use a single pointer variable regardless of list size
vs Alternative: Compared to recursive printing which uses O(n) space on call stack, this iterative approach is more space efficient
Edge Cases
Empty list (head is NULL)
No output is printed, function exits immediately
DSA C
while (current != NULL) {
Single node list
Prints the single node's value and stops
DSA C
while (current != NULL) {
When to Use This Pattern
When you need to visit every item in a linked list in order, use traversal with a pointer moving from head to null.
Common Mistakes
Mistake: Not checking for NULL before accessing node data causing crashes
Fix: Always check current != NULL in the loop condition before accessing current->data
Mistake: Advancing pointer before printing causing skipped nodes
Fix: Print current node's data before moving pointer to next
Summary
It visits each node from start to end and prints its value.
Use it when you want to see or process all elements in a linked list.
Remember to move step-by-step from one node to the next until you reach the end.