Simple linked list, is element fumction! (m && n) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Simple linked list, is element fumction! (m && n)

Why do you use "m && n" instead of just "n" for comparison in this case of a is element function of a simple linked list? int list_iselem(list m, int n) { while(m && n != m->element) { m = m->next; } return (m && n == m->element); }

26th Mar 2018, 12:44 AM
TwoHands
TwoHands - avatar
2 Answers
+ 2
m is your pointer. If it is null, it doesn't point to anything and is also 0/false. If it does point to something, it is non-zero or true.
26th Mar 2018, 1:05 AM
John Wells
John Wells - avatar
+ 1
Oh thank you, that makes sense. I guess I confused the prioritys there too. != has the higher priority than && so if the left statement is unequal and the right is true, the loop gets executed.
26th Mar 2018, 8:37 AM
TwoHands
TwoHands - avatar