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