nSack
nSack

Reputation: 65

Program to create a linked list not working

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;
}

Upvotes: 0

Views: 218

Answers (3)

Kalm Tanir
Kalm Tanir

Reputation: 1

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;}.

Upvotes: 0

Shalu Agrawal
Shalu Agrawal

Reputation: 1

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;
}

Upvotes: 0

Dv6
Dv6

Reputation: 489

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.

Upvotes: 2

Related Questions