Linked List 2012

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

  1. start
  2. Set temporary pointer to the first node
  3. Repeat steps 4 and 5 until the pointer reaches null
  4. if the names matches with the parameter value then Print details, stop
  5. else
  6. Move temporary pointer to the next node
  7. 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”)’

}

This entry was posted in Linked list. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *