Shoulda known I'd blow it again. So let's see if I get this straight, finally. (Well, at least 'til I try to work with C style lists or other intense uses of pointers).
if instead of this:
Code:
node * create_node()
{
node * top=new node;
if(top==NULL)
abort();
top->next = NULL;
return top;
}
it had been this:
Code:
void create_node(node * top_ptr)
{
top_ptr = new node;
if(top==NULL)
abort();
top_ptr->next = NULL;
}
it would be an error since top_ptr is being re-directed. In this scenario you need to send top_ptr by reference as in this example:
Code:
void create_node(node ** topPtr)
{
*topPtr = new node;
if(*topPtr == NULL)
abort();
(*topPtr)->next = NULL;
}
Returning a node pointer rather than using a node pointer as a parameter when trying to change the address in the pointer makes the syntax a little cleaner.
However, here:
Code:
void input_data(node *node)
{
cout<<"Input name->";
cin>>node->name;
cout<<'\n'<<"Input age->";
cin>>node->age;
}
the node pointer is used to change the value at the address it is pointing to, not change the address the node is pointing to. Therefore, the pointer doesn't need to be passed by reference, it _is_ the "reference" used to change the value.