samedi 20 août 2016

Why does temp->next!=NULL work while temp!=NULL doesn't in Linked List in C++

Why does the following doesn't work? I have dry run the program and according to my knowledge, when the temp reaches NULL, (new)ing it should create a new node with temp pointing towards it. Oddly, changing the while condition to temp->next!=NULL works perfectly. Any explanation to this?

void addNode(int data){
node *temp=new node;
temp=head;
while(temp!=NULL){
    temp=temp->next;
}
temp=new node;
temp->x=data;
temp->next=NULL;

}

Aucun commentaire:

Enregistrer un commentaire