1

In the code given below, im passing head-pointer from main funtion to addNode function, so that I reserve the position of head-pointer(also pass it to other linkedList related functions to perform other operations) but code below doesnt work as expect, every time I call function addNode, I get Head node Created, am I not passing the pointer to addNode correctly ? How do I achieve my objective to reserve head pointer to list, and send it to addNode function from main() ?

using namespace std;

struct stud {
    string stud_name;
    string stud_roll:
    stud *next_node;
};

void addNode(stud* head);


int main()
{   stud *head = nullptr;
    addNode(head);
    addNode(head);
    addNode(head);
    addNode(head);
    addNode(head);
}

void addNode(stud* head)
{

    stud *new_node = new stud;
    new_node->next_node = NULL;

    if (head == NULL)
    {
        head = new_node;
        cout << "Head node Created" << endl;
    }
    else
    {
        stud *temp_head = NULL;
        temp_head = head;

        while (temp_head->next_node != NULL)
        {
            temp_head = temp_head->next_node;
            cout << "Moving temp pointer" << endl;
        }
        temp_head->next_node = new_node;
        cout << "Body node created" << endl;
    }
}
calculusnoob
  • 165
  • 1
  • 11

1 Answers1

2

What you are doing here is, passing a pointer to a function and allocate it in the function (locally) and you want that local pointer to be accessible and reused out of the function scope. Can you see the problem?

Allocating passed head (coppied when passed) in the function scope will effect function scope. What you can do is passing this pointer by reference. So your code will change like this :

struct stud {
    string stud_name;
    string stud_roll;
    stud *next_node;
};

void addNode(stud *&head);

int main()
{
    stud *head = nullptr;
    addNode(head);
    addNode(head);
    addNode(head);
    addNode(head);
    addNode(head);
}

void addNode(stud *&head)
{

    stud *new_node = new stud;
    new_node->next_node = NULL;

    if (head == NULL)
    {
        head = new_node;
        cout << "Head node Created" << endl;
    }
    else
    {
        stud *temp_head = NULL;
        temp_head = head;

        while (temp_head->next_node != NULL)
        {
            temp_head = temp_head->next_node;
            cout << "Moving temp pointer" << endl;
        }
        temp_head->next_node = new_node;
        cout << "Body node created" << endl;
    }
}
HMD
  • 2,202
  • 6
  • 24
  • 37
  • 2
    You're absolutely correct. The main problem was that, in passing the `head` pointer by value, it's value was never changed outside of `addNode()`. If this were a C program, the solution would be `**head`. You instead chose `&*head`, C++-only "reference to a pointer" syntax. This link explains that syntax - and its implications - in more detail: [Meaning of *& and **& in C++](https://stackoverflow.com/questions/5789806/meaning-of-and-in-c) – paulsm4 Dec 23 '18 at 03:28
  • @paulsm4 Yes it's tagged C++ so I guessed that might be the way to go, Thanks for the link, Nice read. – HMD Dec 23 '18 at 03:33