GATE CSE
First time here? Checkout the FAQ!
x
+1 vote
117 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 (54.9k points)  
edited by | 117 views
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 (37 points)  


Top Users Jun 2017
  1. Bikram

    3912 Points

  2. Arnab Bhadra

    1550 Points

  3. Hemant Parihar

    1502 Points

  4. Niraj Singh 2

    1501 Points

  5. Debashish Deka

    1480 Points

  6. junaid ahmad

    1432 Points

  7. pawan kumarln

    1366 Points

  8. Arjun

    1246 Points

  9. Rupendra Choudhary

    1242 Points

  10. rahul sharma 5

    1240 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 26 - Jul 02
  1. pawan kumarln

    498 Points

  2. akankshadewangan24

    404 Points

  3. Arjun

    286 Points

  4. Debashish Deka

    234 Points

  5. Abhisek Das

    230 Points


23,435 questions
30,153 answers
67,633 comments
28,503 users