Aryan
Aryan

Reputation: 27

Why is this Linked list code result in always head being null?

I have implemented a short linked list code to add to the beginning of the list.

However the head always contained NULL. I really couldn't get why its behaving in this way. Any help is appreciated ! Below is the code :

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

typedef struct node
{
    int iData;
    struct node *next;
} Node;

void add2Beg(Node* head, int num);


int main(int argc, char const *argv[])
{
    Node *head = NULL;
    add2Beg(head, 5);
    if (head == NULL)
        printf("nothing in head !!!\n");
else{
    printf("not null\n");
}
    add2Beg(head, 15);
    return 0;
}

//adds to the beginning of the linked list
void add2Beg(Node* head, int num)
{
    //create a temporary location to hold the new entry
    Node* temp = (Node *)malloc(sizeof(Node));
    temp->iData = num;

    if(head == NULL)
    {
        head = temp;
        printf("inside add2Beg\n");
        printf("%d\n", head->iData);
        head->next = NULL;
        printf("exiting add2Beg\n");
    }
    else
    {
        temp->next = head;
        printf("%p\n", temp->next);
        head = temp;
    }

}

Upvotes: 1

Views: 1735

Answers (3)

Mike
Mike

Reputation: 49483

Because you never assigned head to anything other than NULL...

//here you set it to NULL then pass it to your function
Node *head = NULL; 
add2Beg(head, 5);

here in your function you pass in a copy of "head"

void add2Beg(Node* head, int num)   
{
   //create a temporary location to hold the new entry   
    Node* temp = (Node *)malloc(sizeof(Node));   
    temp->iData = num;      
    if(head == NULL)     //we'll get in here

At this point you assign temp to it, so within the scope of this function it's something vaid, but once you leave this function it's back to NULL.

You passed in a copy of the pointer "head" and called it "head". You need to either return the value and assign it in main() or pass a pointer to head into this function in order for the value to be updated.

Solutions:

Node *head = NULL; 
head = add2Beg(head, 5);

Node* add2Beg(Node* head, int num){
    ...
    return head;

OR

Node *head = NULL; 
add2Beg(&head, 5);

void add2Beg(Node** head, int num){
    ...

Upvotes: 1

Hernan Velasquez
Hernan Velasquez

Reputation: 2820

Your function add2beg is not returning the new head in the case when it's modified. Change your function to:

Node * add2Beg(Node* head, int num)

and return the head at the end:

return head;

Upvotes: 1

cdhowie
cdhowie

Reputation: 169403

Because the head variable inside of add2Beg() is local to that function. Assigning a new pointer value to it (head = temp;) only changes the head variable inside the function. You need to pass in a pointer-to-pointer:

void add2Beg(Node** head, int num)

Then use *head inside the function:

if(*head == NULL)
{
    *head = temp;

Be careful about lines like head->next = NULL; -- this should be rewritten as (*head)->next = NULL; or (**head).next = NULL;.

And so on. Then call the function like so:

add2Beg(&head, 15);

Upvotes: 6

Related Questions