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 [COLOR="#FFD700"]since the pointer has null stored in it it flags an error[/COLOR]
while(tmp2) //made changes here[COLOR="#FF0000"]there is null in tmp2 and so the segment is omitted [/COLOR]
tmp2=tmp2->lpnext;
tmp2=tmp; //made changes here[COLOR="#FF0000"]as the above conditions failed this is also flagged as an error [/COLOR]
}
}
int main()
{
push_me_back(10);
push_me_back(20);
push_me_back(30);
cout<<head->lpnext->num;
getch();
return 0;
}
Let me write another example exactly as above but with pointer to integer.
Code:
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main()
{
int *b,*a,*c;
c=new int;
b=NULL; //it is not pointed any where
a=b; //according to you this is wrong
while(a); //this is also wrong according to you
a=c; //this is also wrong
*a=10; //but all these works well
getch();
return 0;
}
If what you told is correct why does this code run well?
I might not be understanding linklist? If you have any tutorials or site, or anything please help me out.