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

Need help with my JAVA assignment, please! Thanks. Design a program that generat

ID: 3846487 • Letter: N

Question

Need help with my JAVA assignment, please! Thanks.

Design a program that generates a linked list of randomly generated Integer objects. Present a menu at program start that gives the user the following options (most of these options will have corresponding methods in a Linked List class):

1.Create a new list. The size will be specified by the user, make sure a non-negative value is entered. If there is a pre-existing list when this option is chosen, make sure you delete the contents of that list before creating your new list.

2.Sort the list. How you implement this is up to you. You can insert items in the list such that the list is always in sorted order or you can write a sort routine. You may not call a sort routine provided by the Java API.

3.Print the list (to the screen), one number per line.

4.Extra credit – 5 points: Print the list in reverse order (to the screen), one number per line. If you do not attempt the extra credit, make this menu choice display the message “Extra credit not attempted”.

5.Generate a sub-list that contains all the even numbers in the current list. This list should be returned and the contents should be displayed (to the screen), one number per line.

6.Print the contents of every "nth" node in the list. Obtain the "n" from the user, ensure it is greater than 0.

7.Delete node(s) containing an integer value entered by the user. You should report how many were deleted to the user.

8.Delete the contents of the current list.

9.Quit

You may use any linked list implementation you wish (singly linked, doubly linked, dummy head node, circular).

In addition to your LinkedList class, include a driver file called ListTester that will contain the menu and anything else you deem necessary (perhaps a utility to generate random Integers...).

Keep things modular and encapsulate your data properly -- do not let anything outside the Linked List class have direct access to any of the nodes.

Do not accept invalid input of any type from the user and do not let user input crash your program.

Explanation / Answer


import java.util.Scanner;
import java.util.Random;

/* Class Node */
class Node
{
protected int data;
protected Node link;

/* Constructor */
public Node()
{
link = null;
data = 0;
}
/* Constructor */
public Node(int d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public int getData()
{
return data;
}
}

/* Class linkedList */
class linkedList
{
protected Node start;
protected Node end ;
public int size ;

/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
  
/* Function to insert an element at end */
public void insertTask1(int data)
{
Node node = new Node();
node.data = data;

if (start == null) {
start = node;
return;
} else if (node.data < start.data) {
node.link = start;
start = node;
return;
}
Node p = start;

boolean added=false;
while (p.link != null)
{
if (p.link.data > data)
{
node.link = p.link;
p.link = node;
added = true;
break;
}
p = p.link;
}
if (!added)
p.link = node;
size++;
}

public void printTask3()
{
System.out.print(" Singly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (start.getLink() == null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.println(start.getData());
ptr = start.getLink();
while (ptr.getLink() != null)
{
System.out.println(ptr.getData());
ptr = ptr.getLink();
}
System.out.println(ptr.getData());
}
public void printReverseTask4(Node head)
{
// Base case
if (head==null)
return;

// print the list after head node
printReverseTask4(head.getLink());
  
// After everything else is printed, print head
System.out.println(head.getData());
}
public void sublistEvenTask5(Node head)
{
System.out.println("Even list is: ");
if(head==null)
return;

linkedList ll=new linkedList();
if (size == 0)
{
System.out.print("empty ");
return;
}
  
if (start.getLink() == null)
{
if(start.getData()%2==0)
ll.insertTask1(start.getData());
System.out.println(start.getData());
return;
}
Node ptr = start;
  
if(start.getData()%2==0)
{ll.insertTask1(start.getData());
System.out.println(start.getData());
}   
ptr = start.getLink();
while (ptr.getLink() != null)
{
if(start.getData()%2==0)
{ll.insertTask1(start.getData());
System.out.println(start.getData());
} ptr = ptr.getLink();
}
if(start.getData()%2==0)
{ll.insertTask1(start.getData());
System.out.println(start.getData());
}   
}
public void deletevalTask7(int val)
{
if (start.getData()==val)
{
start = start.getLink();
size--;
return ;
}
if (end.getData()==val)
{
Node s = start;
Node t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
Node ptr = start;
int count=0;
for (int i = 1; i < size - 1; i++)
{
if (ptr.getData()==val)
{
Node tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
count++;
break;
}
ptr = ptr.getLink();
}
size-=count;
System.out.println("Number of nodes deleted: "+count);
}
public void printnTask6(int n)
{
int l=0;
Node ptr=start;
for (int i = 1; i < size - 1; i++)
{
if(l==n)
{
System.out.println(ptr.getData());
l=-1;
}
ptr=ptr.getLink();
l++;
}
}
public void currentlistTask8()
{
while(start.getLink()!=null)
{
Node tmp = start.getLink();
tmp = tmp.getLink();
start.setLink(tmp);
size--;
}
System.out.println("Linked List Deleted");
}
}

/* Class SinglyLinkedList */
public class SinglyLinkedList
{
public static void main(String[] args)
{   
Random rand=new Random();
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedList */
linkedList list = new linkedList();
System.out.println("Singly Linked List Test ");
char ch;
/* Perform list operations */
do
{
System.out.println(" Singly Linked List Operations ");
System.out.println("1. Create Linked List");
System.out.println("2. Sort");
System.out.println("3. Display");
System.out.println("4. Print list in reverse");
System.out.println("5. Generate sub list of even numbers");
System.out.println("6. Print value of every Nth node");   
System.out.println("7. Delete a particular value from the list");   
System.out.println("8. Delete contents of currenr list");
System.out.println("9. Quit");
  
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter size of linked list: ");
int len=scan.nextInt();   
if(!(len>0))
System.out.println("Invalid input");
else{
//if there is some pre existing list, to delete the list
list.start=null;
//creating new list
for(int i=0;i<len;i++)
{
list.insertTask1(rand.nextInt(11000)+1);
}}
break;
case 2 :
System.out.println("List is sorted");   
break;   
case 3 :
list.printTask3();
break;
case 4 :
//reverse
list.printReverseTask4(list.start);
break;
case 5 :
list.sublistEvenTask5(list.start);
break;   
case 6 :
System.out.println("Enter value of n");
int n=scan.nextInt();   
list.printnTask6(n);
break;   
case 7 :
System.out.println("Enter vslue to be deleted");
int val=scan.nextInt();   
list.deletevalTask7(val);
break;   
case 8 :
list.currentlistTask8();
break;   
case 9 :
System.exit(0);
default :
System.out.println("Wrong Entry ");
break;   
}
/* Display List */
//list.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');   
}
}

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