Create a linked list using the Node class that contains the first million prime
ID: 666761 • Letter: C
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;
}
}
Extra Credit: Sort your linked list while you are building it.
Explanation / Answer
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;
/**
*
* @author Krishna
*/
public class LinkedListNode {
/**
* @param args the command line arguments
*/
static LinkedList<Node> listOfNodes = new LinkedList<Node>();
private static boolean isListInitiated = false;
private long MAX = 1000;// increase limit - used simple prime generation algo.. which takes time for larger values
ObjectComparator comparator = new ObjectComparator();
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedListNode obj = new LinkedListNode();
obj.initialize();
int choice,element;
printMenu();
choice = input.nextInt();
while(choice!=4){
switch(choice){
case 1:
System.out.println("Enter element to search in List");
element = input.nextInt();
System.out.println("Element found at index : "+obj.search(element));
printMenu();
choice = input.nextInt();
break;
case 2:
System.out.println("Enter element to add to List");
element = input.nextInt();
obj.addNode(element);
printMenu();
choice = input.nextInt();
break;
case 3:
System.out.println("Enter element to delete from List");
element = input.nextInt();
obj.deleteNode(element);
printMenu();
choice = input.nextInt();
break;
case 4:
System.out.println("Thank you!!! Bye");
break;
// case 5:
// System.out.println("List of Nodes : "+ listOfNodes);
// break;
default :
System.out.println("Please enter a Valid menu item!!! ");
choice = input.nextInt();
printMenu();
choice = input.nextInt();
break;
}
}
// System.out.println("list"+ listOfNodes.size());
// System.out.println("list"+ listOfNodes);
}
private void addMillionPrimesToList(){
for(int number = 2; number<=MAX; number++){
//print prime numbers only
if(isPrime(number)){
listOfNodes.add(new Node(number));
}
}
}
public static boolean isPrime(int number){
for(int i=2; i<number; i++){
if(number%i == 0){
return false; //number is divisible so its not prime
}
}
return true; //number is prime now
}
private int search(int val){
int index = -1;
for(Node node:listOfNodes){
if(node.Data==val)
index = listOfNodes.indexOf(node);
}
return index;
}
private void addNode(int val){
listOfNodes.add(new Node(val));
Collections.sort(listOfNodes, comparator);
}
private void deleteNode(int val){
int index = search(val);
listOfNodes.remove(index);
}
public static void printMenu(){
System.out.println(" ====== Menu =====");
System.out.println("1. Search");
System.out.println("2. Add new");
System.out.println("3. Delete");
System.out.println("4. Exit");
// System.out.println("Just to see leentsin list : "+ listOfNodes);
// System.out.println("5. View elements in list");
System.out.println(" ====== Menu =====");
}
// Adding elements to List
private void initialize(){
if(!isListInitiated){
addMillionPrimesToList();
}
}
}
// Comparator to Sort Node objects based on its Data attribute
class ObjectComparator implements Comparator<Node> {
public int compare(Node obj1, Node obj2) {
if(obj1.Data <= obj2.Data)
return -1;
else
return 0;
}
}
/*
* Node Class
*/
class Node
{
int Data;
Node Next;
Node()
{
Next = null;
}
Node(int val){
this.Data = val;
}
// toString overridden to view contents to Node object - it will be called internally to when we print Node object
@Override
public String toString(){
return this.Data+" ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.