سؤال

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