in DS edited by
816 views
1 vote
1 vote

Consider the following linked list :

Which of the following piece of code will insert the node pointed to by $q$ at the end of the list ?

  1. $\text{for (p=list; p !=NULL; p=p → next);}\\ p=q;$

  2. $\text{for (p=list; p !=NULL; p=p → next);}\\ \text{p→next=q;}$

  3. $\text{for (p=list; p→next !=NULL; p=p → next);}\\ p=q;$

  4. $\text{for (p=list; p→next !=NULL; p=p → next);}\\ p→next=q;$

in DS edited by
816 views

2 Comments

Answer should be D.
1
1
Option D
0
0

2 Answers

0 votes
0 votes
To insert a node at last following procedure must be followed:

1. The list is traversed up to the last node.

2. Link field of the last node is updated (now pointing to the new node)

So option (D)  is correct.

Here link field is updated only one time.
0 votes
0 votes
// Reach the last node and stop.

for (p=list; p→next !=NULL; p=p → next);

// Change the next pointer of the last node to point to the NEW NODE.

p→next=q;

Related questions