Search code examples
cloopsstructlinked-listsingly-linked-list

C program to traverse a singly linked list


I have written a C program to implement the concept of traversing a singly linked list. The program first creats the list by asking for the user input and then displays/ traverses through the created list. When i run this code, it sucessfully creates the linked list, but on displaying the created list, it produces an infinite loop. `

#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
} *head;

int main(){
    struct node *newNode, *temp;
    int n;
    printf("Enter number of Nodes: ");
    scanf("%d",&n);
    if(n <= 1){
        printf("Invalid Input Nodes should Nodes should be greater than 1");
        exit(0);
        
        }
        else{
    head = (struct node *)malloc(sizeof(struct node));
    if(head == NULL){
        printf("No Memory Allocation");
        exit(0);
        }
        else{
            printf("Node Data 1: ");
            scanf("%d",&head -> data);
            head -> next = NULL;
            temp = head;
            }
    newNode = (struct node *)malloc(sizeof(struct node));
    if(newNode == NULL){
        printf("No Memory Allocation");
        exit(0);
        }
        else{
        for(int i = 2; i <= n; ++i){
            printf("Node Data %d: ",i);
            scanf("%d",&newNode -> data);
            newNode -> next = NULL; 
            temp -> next = newNode;  
            temp = temp -> next;
            
            }
        }
        
    //Traversal        
       struct node *ptr;     
        ptr = head;   
        if(ptr == NULL)  
        {  
            printf("Empty list..");  
        }  
        else  
        {   
            while (ptr != NULL){  
                printf("\n%d",ptr->data);  
                ptr = ptr -> next;  
            }  
        }  
    }
    return 0;
}

`


Solution

  • In this for loop

        for(int i = 2; i <= n; ++i){
            printf("Node Data %d: ",i);
            scanf("%d",&newNode -> data);
            newNode -> next = NULL;
            temp -> next = newNode;
            
            }
    

    the same pointer newNode is assigned to the data member temp->next that is equivalent to the expression head->next because within the loop the pointer temp is not changed.

    You need to allocate new nodes in the loop and reassign the pointer temp.

    Also you need to check the entered value for the variable n. For example if n is set to 1 then there will be a memory leak.

    And there is neither infinite loop provided that the entered value for the variable n is not equal to INT_MAX.

    The program can look the following way

    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
        struct node 
        {
            int data;
            struct node *next;
        } *head = NULL;
    
        unsigned int n = 0;
    
        printf( "Enter number of Nodes: " );
        scanf( "%u", &n );
    
        if (n != 0)
        {
            head = malloc( sizeof( struct node ) );
    
            if (!head)
            {
                puts( "No Memory Allocation" );
                exit( 0 );
            }
            else 
            {
                printf( "Node Data 1: " );
                scanf( "%d", &head->data );
                head->next = NULL;
            
                unsigned int i = 0;
                for (struct node *temp = head;  ++i < n; temp = temp->next)
                {
                    if (( temp->next = malloc( sizeof( struct node ) ) ) == NULL)
                    {
                        puts( "No Memory Allocation" );
                        break;
                    }
                    temp->next->next = NULL;
                    printf( "Node Data %u: ", i + 1 );
                    scanf( "%d", &temp->next->data );
                }
    
                for (const struct node *current = head; current != NULL; current = current->next)
                {
                    printf( "%d -> ", current->data );
                }
    
                puts( "null" );
            }
        }
    }
    

    The program output is

    Enter number of Nodes: 5
    Node Data 1: 1
    Node Data 2: 2
    Node Data 3: 3
    Node Data 4: 4
    Node Data 5: 5
    1 -> 2 -> 3 -> 4 -> 5 -> null
    

    Pay attention to that you need to append the program with the code that will free all the allocated memory.