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 ?