What is traversing and why is it needed?
Traversing means visiting all the nodes in the linked list from the beginning to the end and working with their data such as: printing, searching, summation, etc.
This is an operation that can perform complex operations like adding new nodes to a linked list, deleting them, or finding a specific node.
Traversing Algorithm
I have explained to you below how the traversing algorithm works. Its logic is very simple:
- Taking a cursor: Take a current node pointer and set it to the head node.
(current = head) - Running a loop: The current node will continue to print data until the current node becomes NULL. Move the current node to its next node.
(current = current->next)
How to traverse singly linked in C programming:
#include
#include
//Define the structure of a node
struct node{
int data; //Data stored in the node
struct node *next; //Address of the next node
};
//Function to the traverse and print the linked list
void printLinkedList(struct node *head){
//1. create a current pointer and set it to head
struct node *current = head;
printf("Linked List: ");
//2. run the loop until current becomes NULL
while(current != NULL){
//print the data of the current node
printf("%d -> ",current->data);
//Move the current pointer to the next node
current = current->next;
}
//At the end of the list, we get NULL
printf("NULL\n");
}
int main(){
//Manually create a few nodes
struct node *head = NULL;
struct node *second = NULL;
struct node *third = NULL;
//Allocate memory for nodes
head = (struct node*)malloc(sizeof(struct node));
second = (struct node*)malloc(sizeof(struct node));
third = (struct node*)malloc(sizeof(struct node));
//Set data and connection for nodes
head->data = 10; //data of the first node
head->next = second; //first node point to second node
second->data = 20; //data of the second node
second->next = third; //second node point to third node
third->data = 30; // Data of the third node
third->next = NULL; //Third node point to NULL, meaning end of list
//call the function to print the linked list
printLinkedList(head);
return 0;
}
Code Explanation
struct node:With this structure we have created a blueprint of nodes. Each node will have aninttypedataand a pointernextto it.printLinkedListFunction:- struct Node* current = head; : I took a pointer variable called
currentand placed it at theheadof the list. while(current != NULL):whileis the main loop of this function. It will run until thecurrentreachesNULL.printf("%d -> ", cureent->data);:This code prints the data of the current node inside the loop.current = current->next;:This is the most important step of this code. This code is moving thecurrentpointer to the next node. The value ofcurrent->nextis the address of the next node.
- struct Node* current = head; : I took a pointer variable called
main Function:In the main function, we created three nodes, established connections between them, and finallyprntLinkedListcalled the function to print the entire list.
output:
Linked List: 10 -> 20 -> 30 -> NULL
