Traversing in singly linked list

The process of visiting each node of the list once to perform some operation on it is called traversing. It is performed in almost every scenario of the singly linked list and is the most common operation, for which we can use the below statements.

ptr = head;   
while (ptr!=NULL)  
{  
  ptr = ptr -> next;  
}

Algorithm:

  • STEP 1: SET PTR = HEAD
  • STEP 2:IF PTR = NULL
    WRITE “LIST IS EMPTY”
    GOTO STEP 7
    END OF IF
  • STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR != NULL
  • STEP 5: PRINT PTR→ DATA
  • STEP 6:PTR = PTR → NEXT
    [END OF LOOP]
  • STEP 7: EXIT

Example in C:

#include<stdio.h>  
#include<stdlib.h>  
void createNode(int);  
void traverse();  
struct node  
{  
    int data;  
    struct node *next;  
};  
struct node *head;  
void main ()  
{  
    int choice,item;  
    do   
    {  
        printf("\n1.Insert Node\n2.Traverse\n3.Exit\n4.Enter your choice:\n");  
        scanf("%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            printf("\nEnter the element to insert:\n");  
            scanf("%d",&item);  
            createNode(item);  
            break;   
            case 2:  
            traverse();  
            break;   
            case 3:  
            exit(0);  
            break;    
            default:  
            printf("\nPlease enter a valid choice.\n");  
        }  
 
    }while(choice != 3);  
}  
void createNode(int item)  
    {  
        struct node *ptr = (struct node *)malloc(sizeof(struct node *));  
        if(ptr == NULL)  
        {  
            printf("\nOVERFLOW\n");  
        }  
        else  
        {  
            ptr->data = item;  
            ptr->next = head;  
            head = ptr;  
            printf("\nNode inserted successfully!!\n");  
        }  
 
    }  
void traverse()  
    {  
        struct node *ptr;     
        ptr = head;   
        if(ptr == NULL)  
        {  
            printf("List is Empty.");  
        }  
        else  
        {  
            printf("printing values . . . . .\n");   
            while (ptr!=NULL)  
            {  
                printf("\n%d",ptr->data);  
                ptr = ptr -> next;  
            }  
        }  
     }

Output:

1.Insert Node 
2.Traverse 
3.Exit 
4.Enter your choice: 
1 
 
Enter the element to insert: 
2 
 
Node inserted successfully!! 
 
1.Insert Node 
2.Traverse 
3.Exit 
4.Enter your choice: 
1 
 
Enter the element to insert: 
4 
 
Node inserted successfully!! 
 
1.Insert Node 
2.Traverse 
3.Exit 
4.Enter your choice: 
1 
 
Enter the element to insert: 
6 
 
Node inserted successfully!! 
 
1.Insert Node 
2.Traverse 
3.Exit 
4.Enter your choice: 
2 
printing values . . . . . 
 
6 
4 
2 
1.Insert Node 
2.Traverse 
3.Exit 
4.Enter your choice: 
1 
 
Enter the element to insert: 
8 
 
Node inserted successfully!! 
 
1.Insert Node 
2.Traverse 
3.Exit 
4.Enter your choice: 
2 
printing values . . . . . 
 
8 
6 
4 
2 
1.Insert Node 
2.Traverse 
3.Exit 
4.Enter your choice: 
3
Please Share