1

I've written a function for adding to the end of a singly linked list in C. But what I don't get is why if the head element is NULL, why it continues remaining to be NULL after successive adds.

The struct is defined as this:

typedef struct node* node;

struct node {
    int data;
    node next;
}

In the main I have this:

node test = NULL;
add(test,1);
add(test,2);
add(test,3);

function add is defined as such:

void add(node head, int newData) {
    node n = createNode(newData);
    if (head==NULL) {
        head = n;
        return;
    }
    else {
        node tmp = head;
        while (tmp->next != NULL) {
           tmp = tmp->next;
        }
        tmp = n;
    }
}

createNode is defined as thus:

node createNode(int data) {
    node n = (node) malloc(sizeof(struct node));
    n->next = NULL;
    n->data = data;
    return n;
}

What I am confused about is that, the add function works fine if I first initialize the head (node test = createNode(1)) and then proceeds to add the rest of the values alright. But if I leave the test node to be NULL, it doesn't add any values? What is happening here?

4
  • pass node* to your function add, it will work. Also, u need to change the code in ur function accordingly . Commented Jul 5, 2015 at 15:11
  • @jpw; He is right. He should have pass pointer to pointer. Commented Jul 5, 2015 at 15:12
  • 1
    But for the function to work u need to pass a double pointer. Well, i will try to write a proper answer to this. Commented Jul 5, 2015 at 15:12
  • 1
    Hint: don't typedef pointers, but the struct. This will create less confusion. Commented Jul 5, 2015 at 15:27

1 Answer 1

2

Write function add the following way

void add( node *head, int newData ) 
{
    node n = createNode( newData );

    while ( *head ) head = &( *head )->next;

    *head = n;
}

or you can write even the following way

void add( node *head, int newData ) 
{
    while ( *head ) head = &( *head )->next;

    *head = createNode( newData );
}

and call it like

node test = NULL;

add( &test, 1 );
add( &test, 2 );
add( &test, 3 );

Take into account that function createNode must be declared before function add and you missed a semicolon in the structure definition

struct node {
    int data;
    node next;
}
^^^

Also it is not a good idea to use the same identifier for a struture tag and pointer to the same structure

typedef struct node* node;

At least it would be better to write something like

typedef struct node* node_ptr;
Sign up to request clarification or add additional context in comments.

2 Comments

The issue is due to scope, so you are right, also I just found this answer: stackoverflow.com/questions/25780563/… Thanks!
@roostersign Of course function createNode should be declared before function add.

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.