-1

I'm facing with a trouble of Node. I cannot traversal Node list after add a new node at front of list. Result of adding ain't action. Unless I put body of function "Add_a_another_Node_in_first_list" inside function main then it work. Anyone can explain to me why?

#include<iostream>
using namespace std;
struct LinkedList
{
    int data;
    struct LinkedList *pnext;

}; typedef struct LinkedList Node;

void Create_a_Node(int data, Node *head)
{
    head->data = data;
    head->pnext = NULL;
}
void Add_a_Node_in_first_list(Node *second, Node *head, Node *third)
{
    head->pnext = second;
    head->data = 0;
    second->data = 1;
    second->pnext = third;
    third->data = 2;
    third->pnext = NULL;
}

void Add_a_another_Node_in_first_list(int data, Node *head)
{
    Node *new_node = new Node;
    new_node->data = data;
    new_node->pnext = head;
    head=new_node;

}
void Traversal_Nodes(Node *ptr)
{
    while (ptr != NULL)
    {
        cout << ptr->data << "\t" << ptr;
        cout << endl;
        ptr = ptr->pnext;
    }

}
int main()
{
    Node *head = NULL;
    Node *second = NULL;
    Node *third = NULL;
    head = new Node;
    second = new Node;
    third = new Node;
    Create_a_Node(1, head);
    Add_a_Node_in_first_list(second,head, third);
    Traversal_Nodes(head);
    Add_a_another_Node_in_first_list(-1, head);
    cout << "\nAfterwards\n";
    Traversal_Nodes(head);

} 
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
Nagai Kei
  • 23
  • 1
  • 1
  • 6
  • 4
    Despite what you think, full caps in your title is how you get people to avoid your issue, not look at it. – searchengine27 Jun 12 '20 at 15:25
  • @searchengine27 Not kind! – nicomp Jun 12 '20 at 15:25
  • 2
    @nicomp I'd say shouting at me with full caps is not kind either. – Yksisarvinen Jun 12 '20 at 15:27
  • 2
    Read about how to modify things passed to functions. You need to add a level of pointer. So pass a pointer to pointer to your function, and modify the dereference of said argument. Or better: since it's C++, pass a reference-to-pointer. Or, in either language, you could just pass a pointer by value and return the new pointer from the function. All of these are basics that a decent book or tutorial should walk you through. – underscore_d Jun 12 '20 at 15:28
  • 1
    To expand on above comment: You are modifying a copy of a pointer. Both pointers point to the same object, but pointer variables themselves are different. To remedy that you can use pointer-to-pointer or pass the pointers by reference. – Yksisarvinen Jun 12 '20 at 15:29
  • Does this answer your question? [Function does not change passed pointer C++](https://stackoverflow.com/questions/11842416/function-does-not-change-passed-pointer-c) – underscore_d Jun 12 '20 at 15:31

1 Answers1

0

While calling the function Add_a_another_Node_in_first_list(), you are changing the value of head. Therefore pass reference of the head pointer.

Have a look at the following implementation:

#include<iostream>
using namespace std;
struct LinkedList
{
    int data;
    struct LinkedList *pnext;

}; typedef struct LinkedList Node;

void Create_a_Node(int data, Node *head)
{
    head->data = data;
    head->pnext = NULL;
}
void Add_a_Node_in_first_list(Node *second, Node *head, Node *third)
{
    head->pnext = second;
    head->data = 0;
    second->data = 1;
    second->pnext = third;
    third->data = 2;
    third->pnext = NULL;
}

void Add_a_another_Node_in_first_list(int data, Node* *head)
{
    Node* new_node = new Node;
    new_node->data = data;
    new_node->pnext = *head;
    *head=new_node;

}
void Traversal_Nodes(Node *ptr)
{
    while (ptr != NULL)
    {
        cout << ptr->data << "\t" << ptr;
        cout << endl;
        ptr = ptr->pnext;
    }

}
int main()
{
    Node *head = NULL;
    Node *second = NULL;
    Node *third = NULL;
    head = new Node;
    second = new Node;
    third = new Node;
    Create_a_Node(1, head);
    Add_a_Node_in_first_list(second,head, third);
    Traversal_Nodes(head);
    Add_a_another_Node_in_first_list(-1, &head);
    cout << "\nAfterwards\n";
    Traversal_Nodes(head);

} 

Output:

0    0x171e670
1    0x171e5b0
2    0x171e4d0

Afterwards
-1    0x171e410
0    0x171e670
1    0x171e5b0
2    0x171e4d0
Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35
  • Why above 2 ways of reference is difference whereas you don't use address of head pointer , but you still use address of which head pointer holding at Function "Add_a_another_Node_in_first_list". Please explain to me. Thanks you – Nagai Kei Jun 13 '20 at 05:00