Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that performs the following tasks: 1) Repeatedly prompts th

ID: 3740766 • Letter: W

Question

Write a Java program that performs the following tasks:

1) Repeatedly prompts the user for a name which will be a String. Store the name in an appropriate data structure to create a list of names. Develop a way to exit the repeated prompt.

2) Ask the user the user for a name to search for in the data structure. Return the position of where the name is in the list and print the position to the user.

3) The previous 2 tasks should occur in a menu that will let the user either add more names or search for names.

Explanation / Answer

// Iterative Java program to search an element

// in linked list

// Java program to demonstrate BufferedReader

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

//Node class

class Node

{

String data;

Node next;

Node(String d)

{

data =new String (d);

next = null;

}

}

//Linked list class

class LinkedList

{

Node head; //Head of list

//Inserts a new node at the front of the list

public void push(String new_data)

{

//Allocate new node and putting data

Node new_node = new Node(new_data);

//Make next of new node as head

new_node.next = head;

//Move the head to point to new Node

head = new_node;

}

//Checks whether the value x is present in linked list

public int search(Node head, String x)

{

Node current = head; //Initialize current

int y=0;

while (current != null)

{

if (current.data.equalsIgnoreCase(x))

return y; //data found

current = current.next;

y++;

}

return -1; //data not found

}

//Driver function to test the above functions

public static void main(String args[])throws IOException

{

//Start with the empty list

LinkedList llist = new LinkedList();

//Enter data using BufferReader

BufferedReader reader =

new BufferedReader(new InputStreamReader(System.in));

int i;

// Reading data using readLine

String name;

do{

System.out.println("Enter 1 for input String");

System.out.println("Enter 2 for search String");

i = Integer.parseInt(reader.readLine());

switch(i){

case 1:

System.out.println("Enter name: ");

name = reader.readLine();

llist.push(name);

break;

case 2:

System.out.println("Enter String name to search: ");

name = reader.readLine();

int z=llist.search(llist.head, "21");

if (z>=0)

System.out.println("Yes and location is "+z);

else

System.out.println("No");

break;

default:System.out.println("Wrong Choice");

}

}while(i<3);

  

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote