0

I was trying to solve a question to insert a node into a sorted double linked list To insert an element to the end of the list I used a simple loop in which the pointer move till the element to insert is less than the one in the list or it hits NULL. My loop works when the condition is

while ( cur !=NULL && cur->data < data ) {
    prev = cur ; 
    cur = cur->next; 
}

But doesn't work when the condition is

while ( cur->data < data && cur !=NULL ) {
    prev = cur ; 
    cur = cur->next; 
}

can someone explain why this happens ?

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
StackUser2204
  • 126
  • 1
  • 6
  • In second case, do you get sigmentation fault? – mazhar islam Jul 05 '15 at 05:05
  • If `cur` is actually null then the second case will dereference a null pointer. The thing on the left of `&&` happens before the thing on the right. – M.M Jul 05 '15 at 05:06
  • Or this duplicate: http://stackoverflow.com/questions/628526/is-short-circuiting-boolean-operators-mandated-in-c-c-and-evaluation-order – Bill Lynch Jul 05 '15 at 05:10

0 Answers0