Traversing a single linked list

Traversing a singly linked list means visiting every node of the list in order to perform a certain operation on each node, such as printing its value or updating its data. Here's an example of how to traverse a singly linked list using C:

```
void traverseList(struct Node* head) {
    if (head == NULL) {
        printf("List is empty!");
        return;
    }
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
}
```

In this code, `head` is the pointer to the first node of the list. We start by checking if the list is empty. If it is, we print a message and return. Otherwise, we initialize a temporary pointer `temp` to point to the first node of the list. We then loop through the list by repeatedly following the `next` pointer of the current node and printing its `data` value. Finally, we exit the loop when `temp` becomes `NULL`, which means we have reached the end of the list.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext