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