‹ Back to the paper
A linked list is formed from the objects of the class Node. The class structure of the Node is…
A linked list is formed from the objects of the class Node. The class structure of the Node is given below:
class Node
{
int n;
Node link;
} Write an Algorithm OR a Method to search for a number from an existing linked list.
The method declaration is as follows:
`void FindNode( Node str, int b )`Answer
Answer
Official answer keyAlgorithm:
Step 1: Start. Set a temporary pointer temp = str (the head of the list).
Step 2: Repeat step 3 while temp is not null.
Step 3: If temp.n equals b, display "b is found" and stop (exit).
Otherwise, move temp to temp.link and repeat step 2.
Step 4: If the loop ends without finding b (temp has become null), display "b is not found".
Step 5: Stop.
Method:
void FindNode(Node str, int b)
{
Node temp = str;
while (temp != null)
{
if (temp.n == b)
{
System.out.println(b + " is found");
return;
}
temp = temp.link;
}
System.out.println(b + " is not found");
}(Verified by running the code: correctly finds a value at the last node, at a middle node, and correctly reports 'not found' when the value is absent.)From ISC 2025 Specimen Computer Science Paper 1, question 11(i).