#include <iostream>
usingnamespace std;
class node{
public:
int value;
node *next;
void insert_front(node *&head, int y);
};
void insert_front(node *&head, int y)
{
node *temp;
temp = new node;
temp->value = y;
temp->next = NULL;
if(head == NULL){
temp->next = NULL;
head = temp;
}
else{
temp->next = head;
head = temp;
}
}
int main()
{
node *head = NULL;
int x;
insert_front(head, 2);
insert_front(head, 3);
insert_front(head, 4);
for(node *j=head; j->next!=NULL; j=j->next)
cout<<j->next->value<<endl;
system ("Pause");
return 0;
}
The problem is, It doesnt insert the last inserted value; or doesnt display last inserted value.
That is in here, 4 doesnt display in between inserted values 2,3,4(4 is the last inserted value). Or if I insert just 2(2 is last inserted value since its the only inserted value), it doesnt display anything.
Where I made mistake???