문제

Edit: Original problem fixed.

New problem: While loop doesn't break for or statement:

while(m->next != NULL || m->val != n)
{ 
    cout<<"Looking for main node. Comparing"<<n<<" to "<<m->val<<endl;
    m = m->next;
}    

It prints out all the comparisons, including the two that are exactly alike. Any reason why this wouldn't be breaking it?

도움이 되었습니까?

해결책

m = NULL is assignment statement, m == NULL is the comparison statement to be used in your if statement

Note: Checking m for NULL should be done before using it for even printing (in cout)

If you want to continue the while loop till the last element or till val equals n, then it should be like this

while(m != NULL && m->val != n)
{ 
    cout<<"Looking for main node. Comparing"<<n<<" to "<<m->val<<endl;
    m = m->next;
} 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top