0

Only the head element gets printed and no element gets printed further.

My guess is that the code inside else{.....} inside the createLinkedList(int n) function portion isn't seem to be doing it's job.

Code is below :

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

struct Node
{
    int data;
    struct Node * next;
}*head;

void createLinkedList(int n)
{
    int i;
    struct Node *temp, *p;
    
    for(i=1; i<=n; i++)
    {
        // First thing we do is create an ISOLATED NODE WHICH IS NOT LINKED TO ANYTHING 
        
        temp = (struct Node *)malloc(sizeof(struct Node));
        printf("Now enter your element %d of linked list: ",i);
        scanf("%d",&(temp->data));
        temp->next = NULL;
        
        // ISOLATED NODE CREATED. SINCE THIS IS UNLINKED, WE MUST STORE IT'S POINTER VARIABLE TO NULL
        
        
        if(head == NULL) // Meaning our Linked List is empty
            head = temp; 
        
        else // Meaning our Linked List is not empty. Has at least one element.
        {
            p = head;
            while(p != NULL)
                p = p->next;  // Accessing Last pointer of Linked List
            
            p = temp; // THAT IOSOLATED NODE Is Hereby Connected to our final Linked LIST Element
        }
        printf("\n");
    }
}

int main() 
{
    head = NULL;
    printf("Enter the length of your linked list: ");
    int n;
    scanf("%d", &n);
    printf("\n");
    createLinkedList(n);
    return 0;
}

3 Answers 3

2

Change while(p != NULL) p = p->next; to

while(p->next != NULL)
            p = p->next; 

That would give you the last pointer position where you can insert your node. Right now, p will be null at the end of the iteration.

After that you need to do p->next = temp so that your newly created node is added to the linked list.

Sign up to request clarification or add additional context in comments.

3 Comments

but the code ` p->next ` is obviously also pointing to the null pointer. Mine code is just giving an extra iteration and reaching the same result.
Your code will run until p is null and then when you do p = temp, p will store the address of temp that's all. In You need to have the address of the last node while inserting, and the last node's next won't be pointing to any other node. So you need to traverse the list till you find a node whose next is null. That would give you the address of the last node after that all you have to do is make last node's next pointer point to the newly added node.
Your result is p is NULL. What happens on the next line where you assign p = temp? That's equivalent to NULL = temp, and that's not what you want.
0

Instead of looping in else block, we can track the tail pointer for last node in linked list. Below is modified code:

void createLinkedList(int n)
{
    int i;
    struct Node *temp, *tail;
    
    for(i=1; i<=n; i++)
    {
        // First thing we do is create an ISOLATED NODE WHICH IS NOT LINKED TO ANYTHING 
        
        temp = (struct Node *)malloc(sizeof(struct Node));
        printf("Now enter your element %d of linked list: ",i);
        scanf("%d",&(temp->data));
        temp->next = NULL;
        
        // ISOLATED NODE CREATED. SINCE THIS IS UNLINKED, WE MUST STORE IT'S POINTER VARIABLE TO NULL
        
        
        if(head == NULL) // Meaning our Linked List is empty
        {
            head = temp;
            tail = temp;
        }
        
        else // Meaning our Linked List is not empty. Has at least one element.
        {
            tail->next = temp; // THAT IOSOLATED NODE Is Hereby Connected to our final Linked LIST Element
            tail = temp;
        }
        printf("\n");
    }
}

int main() 
{
    head = NULL;
    printf("Enter the length of your linked list: ");
    int n;
    scanf("%d", &n);
    printf("\n");
    createLinkedList(n);
    return 0;
}

Comments

0

your code working but you lose the addresses of the next nodes when you are using while(p != NULL) so you should change it to while(p->next != NULL){p=p->next;}{p->next=temp;}.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.