Page 533 - Computer science 868 Class 12
P. 533
8. Write an algorithm/Java code to display the contents of an existing linked list using method: void display(Node
start) where start refers to the start pointer.
Ans. Algorithm
Step 1: Start.
Step 2: Create a new Node ptr and initialise it to start
Step 3: Repeat Step 4 to Step 5 while ptr != null
Step 4: Display ptr.data
Step 5: Assign ptr = ptr.next
Step 6: Stop.
Java Code
void display(Node start)
{
Node ptr=new Node();
ptr=start; // initialise prt to start
while(ptr!=null) // End of linked list
{
System.out.print(ptr.data+" ");
ptr=ptr.next;
}
}
9. Write an algorithm/Java code to search any item from an existing linked list using method: void search(Node
start, int item) where start refers to the start pointer and item is the search value.
Ans. Algorithm
Step 1: Start.
Step 2: Create a new Node ptr and initialise it to start
Step 3: Repeat Step 4 to Step 5 while ptr ! = null
Step 4: If ptr.data = item then display “Number found in the linked list”, go to Step 7
Step 5: Assign ptr = ptr.next
Step 6: If ptr = null then display “Number not found in the linked list”
Step 7: Stop.
Java Code
void search(Node start , int item)
{
Node ptr=new Node(); // initialise prt to start
ptr=start;
while(ptr!=null) // end of linked list
{
if(ptr.data = = item) // if found
System.out.print("Number found in linked list");
531
Data Structures 531

