Remember Me Forgot your Password?

Forum: Electrical & Computer Engineering :: Data Structures & Algorithms :: Linked Lists  New Topic
Problem #10 Reply
Author Message
nak31 Problem #10 Mon 20 Apr 2009 8:42:00 PM
Student
Joined Tues 31 Mar 2009
Posts 511

The following C++ statement(s) are part of the code that removes a node from a linked list.

 

a) Link<Elem>* temp=fence->next; fence->next=fence->next->next;

  if (tail==fence->next) tail=fence; delete temp;

b) Link<Elem>* temp=fence->next; fence->next=fence->next->next;

  if (tail==temp) tail="fence;" delete temp;

c) Link<Elem>* temp=fence->next; fence->next=temp->next;

  if (tail==fence->next) tail=fence; delete temp;

d) Link<Elem>* temp=fence->next; fence->next=fence->next->next;

  if (tail==fence) tail="fence;" delete temp;


Student Rating4/5
nak31 Mon 20 Apr 2009 8:43:00 PM
Student
Joined Tues 31 Mar 2009
Posts 511

Solution 10:

Answer d) is wrong because if tail was equal to fence, then there would have been nothing to the right of the fence to remove.

Answers a) and c) are wrong because we’re checking against fence->next, which has changed on the previous line. Note that a) and c) are equivalent.

Answer b) is the right one.


Student Rating4/5
1
© Copyright SolveMyProblem.Net 2010