Traversing Singly Linked List

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:
  1. Taking a cursor: Take a current node pointer and set it to the head node. (current = head)
  2. 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<stdio.h>
#include<stdlib.h>

//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

  1. struct node:With this structure we have created a blueprint of nodes. Each node will have an int type data and a pointer next to it.
  2. printLinkedListFunction:
    • struct Node* current = head; : I took a pointer variable called current and placed it at the head of the list.
    • while(current != NULL): while is the main loop of this function. It will run until the current reaches NULL.
    • 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 the current pointer to the next node. The value of current->next is the address of the next node.
  3. main Function:In the main function, we created three nodes, established connections between them, and finally prntLinkedList called the function to print the entire list.

output:

				
					Linked List: 10 -> 20 -> 30 -> NULL
				
			
traversing singly linked list

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *