A linked list is formed from the objects of the class
class node
{
int p;
String n;
node next;
}
Write an algorithm/method to search fro a name and display the contents of that node. The method declaration is given below.
void search(node start, String h)
Algorithm to search fo a node in the linked list
Steps
- start
- Set temporary pointer to the first node
- Repeat steps 4 and 5 until the pointer reaches null
- if the names matches with the parameter value then Print details, stop
- else
- Move temporary pointer to the next node
- End
Method to search for a node in a linked list
void search( node start, String b)
{
node a = new node(start)
int f =-1;
while(a!=null && f=-1)
{
if(a.n equals(b))
f=1;
else
a=.a.next;
}
if (f==1)
System.out.println(a.p);
else
System.out.println(“Nose not found”)’
}