The program should be in java and I have attached the code below that needs to b
ID: 3700444 • Letter: T
Question
The program should be in java and I have attached the code below that needs to be edited for the program. Please make sure all 4 options work properly. Create a linked list using the Node class that contains the first million prime numbers (primes4.txt). It should have a menu with these option listed below.
1 = Search for a Number (let the user enter a number to search for)
2 = Add a new Number (let the user enter a number and add it to the head of the list)
3 = Delete a Number (let the user enter a number and delete it if found)
4 = Exit
import java.util.*;
import java.io.*;
class LinkedList
{
public static void main (String[] args) throws IOException
{
/*********************************************************/
File file = new File("primes4.txt");
Scanner infile = new Scanner(file);
Scanner in = new Scanner(System.in);
Node Head = null;
Node Temp, Previous;
int Count = 0;
int A;
while (infile.hasNextInt())
{
A = infile.nextInt();
Count++;
Temp = new Node();
Temp.Data = A;
Temp.Next = Head;
Head = Temp;
}
System.out.println("Finished reading " + Count + " Prime numbers ");
int Choice = 0;
while (Choice != 4)
{
System.out.println(" ");
System.out.println(" 1 = Search for a Number ");
System.out.println(" 2 = Add a new Number ");
System.out.println(" 3 = Delete a Number ");
System.out.println(" 4 = Exit ");
/***************************************************/
System.out.println("Enter your choices");
Choice = in.nextInt();
if(Choice == 1)
{
System.out.println("Enter number to search for:");
A= in.nextInt();
Temp = Head;
while(Temp!=null && Temp.Data!= A)
{
Temp=Temp.Next;
}
if(Temp == null)
System.out.println("Your Number was not found");
else
System.out.println("your number was found");
}
if(Choice == 2)
{
System.out.println("Add");
A=in.nextInt();
Temp = Head;
while(Temp!= null && Temp.Data!= A)
{
Temp=Temp.Next;
}
if(Temp == null)
System.out.println("Your Number was not found");
else
System.out.println("Your Number was found");
}
if(Choice == 3)
{
System.out.println("Delete");
A=in.nextInt();
Temp = Head;
while(Temp!= null && Temp.Data!= A)
{
Previous=Temp;
Temp=Temp.Next;
Previous.Next=Temp.Next;
}
if(Temp == null)
System.out.println("Your Number was not found");
else
{
System.out.println("Your Number was Deleted");
}
}
}
}
}
class Node
{
int Data;
Node Next;
Node()
{
Next=null;
}
}
Explanation / Answer
Here is Working Code
import java.util.*;
import java.io.*;
class LinkedList
{
public static void main(String[] args) throws IOException {
/*********************************************************/
File file = new File("primes4.txt");
Scanner infile = new Scanner(file);
Scanner in = new Scanner(System.in);
Node Head = null;
Node Temp, Previous;
int Count = 0;
int A;
while (infile.hasNextInt()) {
A = infile.nextInt();
Count++;
Temp = new Node();
Temp.Data = A;
Temp.Next = Head;
Head = Temp;
}
System.out.println("Finished reading " + Count + " Prime numbers ");
int Choice = 0;
while (Choice != 4) {
System.out.println(" ");
System.out.println(" 1 = Search for a Number ");
System.out.println(" 2 = Add a new Number ");
System.out.println(" 3 = Delete a Number ");
System.out.println(" 4 = Exit ");
/***************************************************/
System.out.println("Enter your choices");
Choice = in.nextInt();
if (Choice == 1) {
System.out.println("Enter number to search for:");
A = in.nextInt();
Temp = Head;
boolean flag = false;
while (Temp != null) {
if (Temp.Data == A) {
flag = true;
break;
}
Temp = Temp.Next;
}
if (flag = true)
System.out.println("your number was found");
else
System.out.println("Your Number was not found");
}
if (Choice == 2) {
System.out.println("Add");
A = in.nextInt();
Node newNode = new Node();
newNode.Data = A;
newNode.Next = Head;
Temp = newNode;
Head = newNode;
/*
* while (Temp != null) { System.out.println(Temp.Data); Temp =Temp.Next; }
*/
}
if (Choice == 3) {
System.out.println("Delete");
A = in.nextInt();
Temp = Head;
Previous = Head;
boolean flag = false;
while (Temp != null) {
if (Head.Data == A) {
flag = true;
Head = Temp.Next;
} else if (Temp.Data == A) {
flag = true;
if(Temp.Next == null) {
Previous.Next = null;
}else {
Previous.Next = Temp.Next;
}
}
Previous = Temp;
Temp = Temp.Next;
}
if (flag == true)
System.out.println("Your Number was Deleted");
else
System.out.println("Your Number was Not Found");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.