Tonmoy Das
Tonmoy Das

Reputation: 11

Singly Linked List creation problem without using a function

//linked_list_1

#include<stdio.h>
#include<stdlib.h>

struct list
{
    int data;
    struct list *link;
};

int main()
{
    struct list *ll=NULL, *tp=NULL;
    int n=3, i;

    ll=(struct list *)malloc(sizeof(struct list));

    scanf("%d",&ll->data);

    for(i=1; i<n; i++)
    {
        tp=(struct list *)malloc(sizeof(struct list));
        scanf("%d",&tp->data);
        ll->link=tp;
        ll=ll->link;
    }

    ll->link=NULL;

    while(ll)
    {
        printf("\n%d",ll->data);
        ll=ll->link;
    }
}
//linked_list_2

#include<stdio.h>
#include<stdlib.h>

struct list
{
    int data;
    struct list *link;
};

void linked_list(struct list *,int);

int main()
{
    struct list *ll=NULL;
    int n=3;

    ll=(struct list *)malloc(sizeof(struct list));

    linked_list(ll,n);


    while(ll)
    {
        printf("\n%d",ll->data);
        ll=ll->link;
    }
}

void linked_list(struct list *kk, int n)
{
    struct list *tp=NULL;
    int i;
    scanf("%d",&kk->data);
    for(i=1; i<n; i++)
    {
        tp=(struct list *)malloc(sizeof(struct list));
        scanf("%d",&tp->data);
        kk->link=tp;
        kk=kk->link;
    }
    kk->link=NULL;
}

Both the programs are same, the second program is linked list creation using a function: void linked_list(struct list *kk, int n) The first program is also linked list creation but without any function. Second program is working properly but the first program is not working in the right way.

If the inputs are 2 5 4 for both the programs, second program output is 2 5 4 but the first program output is only 4.

Why the first program is not working in the right way? What is the reason?

Upvotes: 1

Views: 198

Answers (1)

Ayan
Ayan

Reputation: 817

The problem with the first program is that you are moving the head of the linked list while you are taking the input. So when you are done taking the inputs basically the head of the linked list is now at the last node of it. Then when you are trying to print out the linked list you are just getting the last node of it since the head got moved to there when you took the last input.

Ideally you should use the temporary pointer, align it with the head pointer of the linked list and then take the input, assign it and move it and keep on building the linked list using it only. The head pointer (here ll) of the linked list once initialized should not be move like this:

ll=ll->link; //Wrong!

Until it is absolutely required to do so. This will result in a truncated linked list and you will lose your nodes.

Ideally your code should be something like this:

ll=(struct list *)malloc(sizeof(struct list));
tp = ll; //Align the temporary pointer with the head of the linked list.
scanf("%d",&tp->data);

for(i=1; i<n; i++)
{
    //Allocate memory to the link of the temporary pointer since it is also a type of struct list.
    tp->link=(struct list *)malloc(sizeof(struct list)); 
    scanf("%d",&tp->link->data);
    tp = tp->link; //Move only the temporary pointer.
}

tp->link=NULL; //Finally assign NULL when done taking inputs.

Notice carefully I am only using the temporary pointer to do everything.

The reason why the second program works is because you are passing the address of the head pointer to the function that you are using to build the linked list. Now this function gets a copy of the address in kk that is the starting address of the linked list and builds the linked list there using it. But back in the main() the ll pointer is still holding the original address where you initially allocated the memory. Hence after constructing the linked list when you are back in the main() to print it, you start with the actual head of the linked list still hold by ll and you are able to print out the entire linked list.

But this still has the same flaw that I mentioned earlier. You are moving the head of the linked list while printing it (i.e. ll).

while(ll)
{
    printf("\n%d",ll->data);
    ll=ll->link; //This is wrong!
}

So after you are done printing the linked list your head of the linked list is now NULL. So if you want to do something with the actual linked list after printing it, it is now not possible since you have made the head NULL. So again the solution is use a temporary pointer align it with the head of the linked list and then use it to print out the linked list. Something like this:

struct list *tp = ll;
while(tp)
{
    printf("\n%d",tp->data);
    tp=tp->link;
}

Upvotes: 1

Related Questions