GATE CSE
First time here? Checkout the FAQ!
x
+1 vote
134 views
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)

asked in Programming by Veteran (64.8k points) 35 220 622
edited by | 134 views
Wha is the code within while() loop?
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?

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.

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?

while(p!=null){

...

}

Here while loop won't execute if list is empty as head i.e p will be null. 

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

Added the last part. Say what will be answer now and why?
Should be option B
how, explain in detail

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.

@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.

1 Answer

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.
answered by (119 points) 1 4


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Top Users Oct 2017
  1. Arjun

    23240 Points

  2. Bikram

    17038 Points

  3. Habibkhan

    7096 Points

  4. srestha

    6008 Points

  5. Debashish Deka

    5430 Points

  6. jothee

    4928 Points

  7. Sachin Mittal 1

    4762 Points

  8. joshi_nitish

    4278 Points

  9. sushmita

    3954 Points

  10. Rishi yadav

    3744 Points


Recent Badges

Popular Question neha singh
Notable Question tajar
Notable Question Imarati Gupta
Notable Question set2018
Popular Question jothee
Notable Question set2018
Notable Question Pavan Kumar Munnam
Notable Question iarnav
Popular Question makhdoom ghaya
Popular Question Satyam
27,254 questions
35,075 answers
83,756 comments
33,185 users