home / study / questions and answers / engineering / computer science / create a
ID: 666860 • Letter: H
Question
home / study / questions and answers / engineering / computer science / create a linked list using the node class that ...
Your question has been answered! Rate it below.
Let us know if you got a helpful answer.
Question
Create a linked list using the Node class that contains the first million prime numbers (primes4.txt). It should have a menu with these options:
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
class Node
{
int Data;
Node Next;
// you may want to also create a constructor that takes the Data as a parameter
Node()
{
Next = null;
}
}
Important !!!!!!!!!!!!!!!!!! must include a line to input the text flie!!!!primes4.txt
Explanation / Answer
import java.util.Scanner;
class Node//given class name
{
protected int Data; //declared data members
protected Node Next;
//creating default constructor
public Node()
{
Data=0;
Next=null;
}
public Node(int Data,Node ni) //**constructor
{
this.Data=Data;
Next=ni;
}
public Node(Node ni) //2nd constructor with parameter
{
this.Data=ni.Data;
Next=null;
}
public void setMydata(int Data)
{this.Data=Data;}
public void setLink(Node ni)
{Next=ni;}
public Node getLink()
{return Next;}
public int getMydata()
{return Data;}
}
public class LinkedList//MAIN METHOD
{
Node start;
int size;
public LinkedList()//Constructor
{
start=null;
size=0;
}
public boolean isEmpty()
{return start==null;}
public int getSize()
{return size;}
public void insertSorted(int Data)
{
Node nn=new Node(Data,null);
size++;
if(start==null)
start
{
start=nn;
start.setLink(null);
}
else if(Data<start.getMydata())//It is not greater than the newnode
{
nn.setLink(start);
start=nn;
}
else //To find the greater element
{
Node m1=start;
Node m2=m1;
while(m1.getMydata()<Data) //replicate loop
{
m2=m1;
m1=m1.getLink(); //move the next node
if(m1==null) break; }
m2.setLink(nn);
nn.setLink(m1);
}
}
public void searchList(int Data)
{
Node t=start;
while(t.getMydata()!=Data)
{
t=t.getLink();
if(t==null) break;
}
if(t==null) //After the null meets error message
System.out.println(Data+" Not Found in list");
else//Pass
System.out.println(Data+" Found in list");
}
public void deleteList(int Data)
{
Node m1=start;
Node m2=m1;
while(m1.getMydata()!=Data ) //repeat until element found
{ m2=m1;
m1=m1.getLink();
if(m1==null) break;//loop reached
}
if(m1==null
System.out.println("No such num to delete");
else
m2.setLink(m1.getLink()); //remove found node m1
}
public void showList()
{
Node t=start;
while(t!=null) //traverse upto null and print each
{
System.out.print("->"+t.getMydata());
t=t.getLink();
}
System.out.println();
}
public static void main(String args[])
{
LinkedList Li1=new LinkedList();
Scanner s=new Scanner(System.in);
int Data,option=0;
while(option!=4) //repeat menu until option 4
{
System.out.println("1.Search for a Num");
System.out.println("2.Add a new Num");
System.out.println("3.Delete a Num");
System.out.println("4.Exit");
System.out.println("Enter your option : ");
option=s.nextInt();
switch(option) //check option expression
{
case 1: //calling searchList
System.out.println("Enter num to search list :");
Data=s.nextInt();
Li1.searchList(Data);
break;
case 2: //calling insert in sorting
System.out.println("Enter num to add list :");
Data=s.nextInt();
Li1.insertSorted(Data);
break;
case 3: //calling delete list
System.out.println("Enter num to Delete list :");
Data=s.nextInt();
Li1.deleteList(Data);
break;
default:
System.out.println("Wrong option...");
}
Li1.showList(); //each operation display list
}
System.out.println("");
System.exit(0);
}
}//end loop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.