1 1 vote p=head; q=head-> next; while(A) { ....................................... } A is the condition to see list is empty or not, which one is valid? a) p!=NULL; b) q!=NULL; c)(p!=NULL)&&(q!=NULL) d)(p!=NULL)||(q!=NULL) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- a) p!=NULL; b) q!=NULL; c)(p!=NULL)&&(q!=NULL) d)(p!=NULL)||(q!=NULL) Programming in C data-structures + – srestha 2.0k views answer comment Share Follow Print See all 11 Comments 11 11 Comments reply Uzumaki Naruto commented Jan 2, 2017 reply Follow flag Wha is the code within while() loop? 0 0 replyShare srestha commented Jan 2, 2017 reply Follow flag Whatever the code is. I just want to get a condition checking for A, to see list is empty or not For that which option is suitable? 0 0 replyShare Uzumaki Naruto commented Jan 2, 2017 reply Follow flag We have options in terms of q as well, so we can't say it independently of the code. head can be implemented in two ways: 1) which contains the data as well 2) which just acts as a pointer to first data node. 1 1 replyShare srestha commented Jan 2, 2017 reply Follow flag I want to see if list is empty or not Here q is next node of head i.e. p. Then what will be the code? 0 0 replyShare Gate Mission 1 commented Jan 2, 2017 reply Follow flag while(p!=null){ ... } Here while loop won't execute if list is empty as head i.e p will be null. 0 0 replyShare PEKKA commented Jan 2, 2017 reply Follow flag if list is nul then head = null head -> next = null // depends on how head -> next is initialised . if list contains one node head = node head -> next = null in the 2nd case option B , C ,D could be true . But list is actually Not null. hence option A is the answer 0 0 replyShare srestha commented Jan 2, 2017 reply Follow flag Added the last part. Say what will be answer now and why? 0 0 replyShare PEKKA commented Jan 3, 2017 reply Follow flag Should be option B 0 0 replyShare srestha commented Jan 3, 2017 reply Follow flag how, explain in detail 0 0 replyShare Uzumaki Naruto commented Jan 3, 2017 reply Follow flag Option B is correct. If q!=Null then p is definitely not equal to Null. The code must not enter the body of while loop if q=Null, this is because, Observe this statement, p = (p->next)? (p->next->next) : (p->next); if at all p=Null then the condition (p->next) gives segmentation fault as it tries to access the next of Null. 1 1 replyShare Gate Mission 1 commented Jan 3, 2017 reply Follow flag @shreya here p moves one by one, while q moves two at a time whenever q is not pointing to last node... so if list will end q can detect it easily [q!=NULL] but the above code will work only if list has atleast one element..consider empty list... then p-> head [which is null for empty list] and q = head->next ..so now while loop condition should give runtime error as q is saying to point next of NULL. 0 0 replyShare Please log in or register to add a comment.
0 0 votes List is empty means there would not be any node in the list. Means the pointer "head" should point to Null. 'q' will be considered if there exist atleast a single node. So all other three option will be eliminated.(As question is talking about empty list.) Option A is correct. Iambruv answered Jan 2, 2017 Iambruv comment Share Follow 0 reply Please log in or register to add a comment.