bhattaroshan
Member level 1
When i write this it works fine
But when i make slight change in code as follows code is not working what is the difference. Is there anything difference in the code:
Code:
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
typedef struct stack
{
int num;
stack *lpnext;
};
stack *head=0;
void push_me_back(int num)
{
stack *tmp,*tmp2;
tmp=new stack;
tmp->num=num;
tmp->lpnext=0;
if(head==0)
{
head=tmp;
}
else
{
tmp2=head;
while(tmp2->lpnext)
tmp2=tmp2->lpnext;
tmp2->lpnext=tmp;
}
}
int main()
{
push_me_back(10);
push_me_back(20);
push_me_back(30);
cout<<head->lpnext->num;
getch();
return 0;
}
But when i make slight change in code as follows code is not working what is the difference. Is there anything difference in the code:
Code:
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
typedef struct stack
{
int num;
stack *lpnext;
};
stack *head=0;
void push_me_back(int num)
{
stack *tmp,*tmp2;
tmp=new stack;
tmp->num=num;
tmp->lpnext=0;
if(head==0)
{
head=tmp;
}
else
{
tmp2=head->lpnext; //made changes here
while(tmp2) //made changes here
tmp2=tmp2->lpnext;
tmp2=tmp; //made changes here
}
}
int main()
{
push_me_back(10);
push_me_back(20);
push_me_back(30);
cout<<head->lpnext->num;
getch();
return 0;
}