edited by
852 views
1 votes
1 votes

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;$

edited by

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

1 votes
1 votes
1 answer
1
go_editor asked Mar 28, 2020
908 views
Consider a rooted tree in which every node has at least three children. What is the minimum number of nodes at level i (i $0$) of the tree ? Assume that the root is at l...
0 votes
0 votes
1 answer
2
go_editor asked Mar 28, 2020
1,713 views
Which of the following data structure is used to implement recursion ?ArraysStacksQueuesLinked lists
1 votes
1 votes
1 answer
3
go_editor asked Mar 28, 2020
1,157 views
The height of a binary tree with $n$ nodes, in the worst case is :$O(log n)$$O(n)$$\Omega(n\log n)$$\Omega(n^2)$
2 votes
2 votes
1 answer
4