PRASHNIKAप्रश्निका
‹ Back to the paper

A linked list is formed from the objects of the class Node. The class structure of the Node is…

Computer Science20182 marksShort answer
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

AI
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. Otherwise, move temp to temp.link and repeat from 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 on a 3-node list: correctly reports a middle value as found and a value absent from the list as not found.)
Data structures

From ISC 2018 Computer Science Paper 1, question 12(a).