here’s my code
class LList{
struct Elem{int data;Elem *next;};
Elem *head;
public:
void Push(int dat){
if(head==NULL){
head=new Elem;
head->data=dat;
head->next=NULL;
} else {
// ......
}
}
But when i use it, it doesn’t work. The problem is it never finds the pointer to be NULL and it should me NULL. Even when I assigning NULL to the pointer in constructor it doesn’t work. Visual Studio gives me error that says I cannot access desired memory location.
Simply initialize
headin the ctor and it should work.Note: IIRC it is considered equally bad style comparing with
== NULLas for example== TRUE… the better (in my not so humble opinion) style isif(!head), but that is cosmetics. So just a note.