Search code examples
cdata-structuresinfinite-loop

Infinite loop in creation of linked list


For creating linked list, I made the new node as a static node instead of a dynamic node and now my display function loop got converted to an infinite loop, Why?

Structure of node

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

The only chnage I made was in this function and it worked fine after the changes too.

node *  createLinkedList(int n)
{
    int i=0;
    node * head=NULL;
    node * temp=NULL;
    node * p=NULL;

    for(i=0;i<n;i++)

I changed my code from here where intead of writing

temp=(node *)malloc(sizeof(node));

I made a static node "x".

    {
        node x;                             // instead of malloc I made a static node. 
        x.next=NULL;
        printf("Enter data in node %d: ",i+1);
        scanf("%d",&(x.data));
        temp=&x;

        if(head == NULL)
        {
            head=temp;
        }
        else
        {
            p=head;
            while(p->next != NULL)
            {
                p=p->next;
            }
            p->next=temp;
        }
    }
    return head;
}

This is the display function, where the changes caused problem

void display(node * p)
{
    node * temp=p;

It is here that is being converted to an infinite loop.

    while(temp->next != NULL)
    {
        printf("\t%d->",temp->data);       //Due to static node this became an infinite loop , Why?
        temp=temp->next;
    }
}

Why is it happening so , while this program was working fine when I dynamically allocated the memory, but after making a static node as new node, an infinite loop is being created ???


Solution

  • The main problem is not in your display function, it is when you creating linklist.

    A variable declared static in a function retains its state so basically it's address will be same always.

    In your case your x is initialised only once and retains the same address always.
    In the first iteration of loop temp will have the same address of x , head will have the same address of x.

            node x;                             // instead of malloc I made a static node. 
            x.next=NULL;
            printf("Enter data in node %d: ",i+1);
            scanf("%d",&(x.data));
            temp=&x;
    
            if(head == NULL)
            {
                head=temp;
            }
    

    Then in second iteration of loop x retains it's old address then temp again same address. This time we go to else part, p will have the address of head which is x's address. Now p->next will point to temp's address which is also x's address.

    else
            {
                p=head;
                while(p->next != NULL)
                {
                    p=p->next;
                }
                p->next=temp;
            }
    

    So x's address is in temp, head, head->next and this is the problem with the static node created by you because you were thinking that it will create new instance every time. Node created by you pointing is to own address.