Question

1. Create a linked list using the Node class that contains the first million prime numbers...

1. 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;
   }
}

Homework Answers

Answer #1

Code

import java.io.*;
import java.util.*;
class Node
{
int Data;
Node Next;

// you may want to also create a constructor that takes the Data as a parameter
Node()
{
Next = null;
}
}
public 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; // Head is the start of the linked list
Node Temp, Previous; // Temp and Previous are used to delete an item from the list
int Count = 0;

// Read in primes4.txt and add each item to the linked list
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 in " + Count + " numbers");

int Choice = 0;
while (Choice != 4)
{   
printMenu();
System.out.print("Enter your choice: ");
Choice = in.nextInt();
if (Choice == 1)
{
System.out.print("\nEnter 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("\nAdd : ");
A = in.nextInt();
  
Temp = new Node();
Temp.Data = A;
Temp.Next = Head;
Head = Temp;
System.out.println(A +" add to the list");
}
if (Choice == 3)
{
System.out.println("\nEnter number to delete: ");
A = in.nextInt();
Temp = Head;
Previous=Temp;
while (Temp != null && Temp.Data != A)
{
Previous = Temp;
Temp = Temp.Next;
}
if (Temp == null)
System.out.println("Your number was NOT found.");
else
{
System.out.println("Your number was deleted!");
Previous.Next=Temp.Next;
}
  
}
  
}
}

private static void printMenu() {
System.out.println("\n1 = Search for a Number (let the user enter a number to search for)\n" +
"2 = Add a new Number (let the user enter a number and add it to the head of the list)\n" +
"3 = Delete a Number (let the user enter a number and delete it if found)\n" +
"4 = Exit");
}
  
}

you didn't provide the primes4.txt so i just created my on with few numbers

output

prime4.txt

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
Using C++ / provide code comments so I can understand. Create a simple linked list program...
Using C++ / provide code comments so I can understand. Create a simple linked list program to create a class list containing class node { void *info; node *next; public: node (void *v) {info = v; next = 0; } void put_next (node *n) {next = n;} node *get_next ( ) {return next;} void *get_info ( ) {return info;} }; Be able to initially fill the list. Provide functions to insert/append nodes and remove nodes from the linked list. Be...
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node...
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node next; } public boolean isEmpty() { return (first == null); } public void push(String item) { // Insert a new node at the beginning of the list. Node oldFirst = first; first = new Node(); first.item = item; first.next = oldFirst; } public String pop() { // Remove the first node from the list and return item. String item = first.item; first = first.next;...