Unusual characteristics of && operator in while loop in linked list deletion in C -
i have been playing around linked list , got know unusual characteristics of while loop , operatoin(i guess so).
here is:
this delet function.i searching key deleted.
void delet(mynode** head,int value) { printf(" deleting... %d\n",value); mynode *temp = *head,*prev; if(temp->val == value) { *head = (*head)->next; free(temp); return; } else { while( temp->val != value && temp != null) { prev = temp; temp=temp->next; } } if(temp == null) { printf("..and %d not in list\n",value ); } else{ prev->next = temp->next; } }
in while loop have been checking condition this. , worked value in list.
if value deleted not in list throw segmentation fault.
while( temp->val != value && temp != null) { prev = temp; temp=temp->next; }
but interesting thing if swap conditions in while loop works without error. is:
while( temp != null && temp->val != value)
i guess swapping conditions in while loop should't affect output.
could body tell me why or going wrong time.
thank time.
the condition
temp->val != value && temp != null
requires temp
not null temp->val
work. first bit being tested. if temp
null crash.
therefore first test if temp
not null, contents pointed temp
.
i.e.
temp != null && temp->val != value
ps: && short circuit operator knows answer stops evaluation
Comments
Post a Comment