Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 11(i)

2 marks

Computer ScienceISC 2025 Specimen Computer Science Paper 1ProgramData structures

Write the program described below.

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 )
Open in the Linked list visualiser

Answer

Answer

Official answer key
Algorithm: 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.)