Hello, I need help for this JAVA homework: Programming Assignment - Linked Lists
ID: 3847261 • Letter: H
Question
Hello, I need help for this JAVA homework:
Programming Assignment - Linked Lists
Minimal Documentation Required (no javadoc)
Purpose
The purpose of this assignment is to introduce you to basic operations on a linked list.
Specifics
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.
To Turn In:
This assignment must be submitted in working order by posted due date (see Canvas for due date.) Submit a zip file with:
1. your source files
2. minimal documentation (be sure and give at least a brief description of the program, document anything that doesn't work.
Thank you.
Explanation / Answer
package Test;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
class Node
{
protected int value;
protected Node link;
/* initilize the default Constructor */
public Node()
{
link = null;
value = 0;
}
/* initilize the Constructor */
public Node(int d,Node n)
{
value = d;
link = n;
}
/* set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* set value to current Node */
public void setData(int d)
{
value = d;
}
/* get link to next node */
public Node getLink()
{
return link;
}
/*get value from current Node */
public int getData()
{
return value;
}
}
class linkedList
{
protected Node head;
public int listsize;
public linkedList()
{
head=null;
listsize = 0;
}
/*check for empty of linkedlkist */
public boolean isEmpty()
{
return head== null;
}
/* get the size of list*/
public int getSize()
{
return listsize;
}
/* Create list */
public void createList(int size)
{
while(size > 0)
{
Random randomNumber= new Random();
//calling insert method to sort and add element to linked list
int val=randomNumber.nextInt(100);
insertwithSorting(val);
size--;
}
}
/* printing the elements */
public void printElemsts()
{
System.out.print(" elements in list ");
if (listsize == 0)
{
System.out.print("list is empty");
return;
}
if (head.getLink() == null)
{
System.out.println(" " +head.getData() );
return;
}
Node ptr = head;
System.out.println(head.getData());
ptr = head.getLink();
while (ptr.getLink() != null)
{
System.out.println(ptr.getData());
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ " ");
}
/* printing the elements in reverse order */
public void printElemstsinReverse()
{
Node revList=head;
reverseList(revList);
System.out.println(revList.getData());
}
public Node reverseList(Node temp) {
if(temp==null || temp.link == null)
return temp;
Node second = temp.link;
temp.link = null;
// System.out.println(second.getData());
Node rest = reverseList(second);
second.link= temp;
return rest;
}
public void insertwithSorting(int val)
{
Node nptr, ptr, tmp = null;
nptr = new Node(val, null);
boolean ins = false;
if (head == null)
head = nptr;
else if (val <= head.getData())
{
nptr.setLink(head);
head = nptr;
}
else
{
tmp = head;
ptr = head.getLink();
while(ptr != null)
{
if (val >= tmp.getData() && val <= ptr.getData())
{
tmp.setLink(nptr);
nptr.setLink(ptr);
ins = true;
break;
}
else
{
tmp = ptr;
ptr = ptr.getLink();
}
}
if (ins == false)
{
tmp.setLink(nptr);
}
}
listsize++;
}
}
public class SortedSinglyLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
//object of linked list
linkedList list = new linkedList();
System.out.println("Sorted Singly Linked List Test ");
char ch;
do
{
System.out.println(" singly Linked List with sorting ");
System.out.println("1. Create Linked List");
System.out.println("2.print the elemnts");
System.out.println("3.print the elemnts in reverse");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("enter list size");
list.createList(scan.nextInt() );
break;
case 2 :
System.out.println("elements in the list");
list.printElemsts();
break;
case 3 :
System.out.println("print elements in reverse order");
list.printElemstsinReverse();
break;
default :
System.out.println("please select the valid Entry ");
break;
}
System.out.println(" Do you want to continue y/n ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.