c++11 - how ! is different from != -
i writing function goal print list in pair of two, eg:
list : 1 2 3 4 5 6
pair list: 2 1 4 3 6 5
i wrote following code:
printpair(){ bool flag = 1; node *temp = new node(); node *temp2 = new node(); temp2 = null; if(!head) { printf("empty list!!"); return 0; } temp = head; while(!temp && !temp->next) //error here 1. { if(!temp2) //error here 2. temp2->next->next = temp->next; temp2 = temp->next; temp->next = temp->next->next; temp2->next = temp; if(flag) { head = temp2; flag = 0; } temp = temp->next; } }
it works fine if replaced :
1. `while(temp !=null && temp->next !=null)` 2. `if(temp2 != null)`
so how !
different !=
?
while(!temp){}
execute when temp has boolean value false.
while(temp != null)
execute if temp has value other null (if temp true, pass)
you want use while(temp != null) because follow -> next references until reach last item in list (temp->next pointing nowhere)
Comments
Post a Comment