Defining a linkedlist of dynamically allocated nodes with mutilple java files. H
ID: 3807933 • Letter: D
Question
Defining a linkedlist of dynamically allocated nodes with mutilple java files. Help creating ListNode java code and the LinkedList code for this java program. The other two files are provided and the code listed at the bottom.
Code for EmptyListException.java:
// Filename: EmptyListException.java
// Defines the Exception - EmptyListException that can be thrown
// when an operation is requested that is not supported for an empty list
//
// This class will be stored in the ExceptionPkg package.
package ExceptionPkg;
public class EmptyListException extends RuntimeException
{
// constructor with no parameter
public EmptyListException ()
{
this ("List");
}
// constructor with .String. parameter
public EmptyListException (String name) {
// call super class constructor
super (name + " is empty");
}
}
------------------------------------------------------------
Code for LinkedListTest.java:
// Filename: LinkedListTest.java
//
// This file contains a main method to create an object of type LinkedList and
// test all the methods defined for the LinkedList class.
// Import the LinkedList and EmptyListException classes from their appropriate packages
import ListPkg.LinkedList;
import ExceptionPkg.EmptyListException;
import java.util.Scanner;
public class LinkedListTest
{
public static void main(String[] args)
{
// Create the linked list object 'myList' to hold Integer objects and print the list
LinkedList<Integer> myList = new LinkedList<Integer>("Integer List");
System.out.println();
myList.print();
System.out.println();
// Add five items to the front of the list and print the list
System.out.println("Adding five Integers to the front of the list");
for(int x = 0; x < 5; x++)
{
myList.insertAtFront(x);
}
printLengthAndList(myList);
// Add five items to the back of the list and print the list
System.out.println("Adding five Integers to the back of the list");
for(int x = 0; x < 5; x++)
{
myList.insertAtBack(x);
}
printLengthAndList(myList);
// remove an item from the front and from the back of the list
// print the list
System.out.println("Removing an item from the front of the list");
try
{
myList.removeFromFront();
}
catch(EmptyListException ELE)
{
System.out.print(ELE.getMessage());
}
printLengthAndList(myList);
System.out.println("Removing an item from the back of the list");
try
{
myList.removeFromBack();
}
catch(EmptyListException ELE)
{
System.out.print(ELE.getMessage());
}
printLengthAndList(myList);
// Try to remove the Integer value 4 from the list
// catch the EmptyListException if thrown
// display the list
System.out.println ("Attempt to remove a value not in the list: 4");
try
{
if(myList.findAndRemove(4))
{
System.out.println("4 removed");
}
else
{
System.out.println("4 not removed");
}
}
catch(EmptyListException ELE)
{
System.out.println(ELE.getMessage());
}
printLengthAndList(myList);
// Try to remove the Integer value 0 from the list
// catch the EmptyListException if thrown
// display the list
System.out.println ("Attempt to remove a value in the list: ");
try
{
if(myList.findAndRemove(0))
{
System.out.println("0 removed");
}
else
{
System.out.println("0 not removed");
}
}
catch(EmptyListException ELE)
{
System.out.println(ELE.getMessage());
}
printLengthAndList(myList);
// Try to find position of an item not in the list
System.out.println("Attempt to find the position of an item not in the list - 5");
int position = myList.findItemPos(5);
if(position!=-1)
{
System.out.printf("Value of 5 found at %d ", position);
}
else
{
System.out.println("Value of 5 not found");
}
// Try to find position of an item in the list
System.out.println(" Attempt to find the position of an item in the list - 0");
position = myList.findItemPos(0);
if(position!=-1)
{
System.out.printf("Value of 0 found at position %d in the list ", position);
}
else
{
System.out.println("Value of 0 not found");
}
// Remove the item at 'position' in the list
System.out.printf(" Value at position %d being removed ", position);
myList.removeItemAt(position);
printLengthAndList(myList);
// Remove the first element in the list - item at position 0
System.out.println("Removing element at position 0");
myList.removeItemAt(0);
printLengthAndList(myList);
// Remove the first element in the list - item at the front
System.out.println("Removing from front");
myList.removeFromFront();
printLengthAndList(myList);
// get the object at position 2 in the list
System.out.println("Getting value at position 2");
Integer temp = myList.getItemAt (2);
System.out.printf("Value at position is %d ", temp);
// Test the 'getItemAt' method for the IndexOutOfBoundsException
System.out.println(" Begin Testing IndexOutOfBoundsException");
try
{
myList.getItemAt(4);
}
catch(IndexOutOfBoundsException IOBE)
{
System.out.println(" getItemAt (index too large): " + IOBE);
}
try
{
myList.getItemAt(-1);
}
catch(IndexOutOfBoundsException IOBE)
{
System.out.println(" getItemAt (index too small): " + IOBE);
}
// Test the 'remove' method for the IndexOutOfBoundsException
try
{
myList.removeItemAt(4);
}
catch(IndexOutOfBoundsException IOBE)
{
System.out.println(" removeItemAt (index too large): " + IOBE);
}
try
{
myList.removeItemAt(-1);
}
catch(IndexOutOfBoundsException IOBE)
{
System.out.println(" removeItemAt (index too small): " + IOBE);
}
System.out.println("End of IndexOutOfBoundsException test ");
// Clear list
System.out.println("If list not empty - Clear the list");
if(!myList.isEmpty())
{
myList.clear();
}
printLengthAndList(myList);
System.out.println(" Begin Testing EmptyListException");
// test removeFromFront, removeFromBack, findAndRemove, removeItemAt and getItemAt with
// the empty list
try
{
myList.removeFromFront();
}
catch(EmptyListException ELE)
{
System.out.println(" removeFromFront: " + ELE.getMessage());
}
try
{
myList.removeFromBack();
}
catch(EmptyListException ELE)
{
System.out.println(" removeFromBack: " + ELE.getMessage());
}
try
{
myList.findAndRemove(0);
}
catch(EmptyListException ELE)
{
System.out.println(" findAndRemove: " + ELE.getMessage());
}
try
{
myList.removeItemAt(0);
}
catch(EmptyListException ELE)
{
System.out.println(" removeItemAt: " + ELE.getMessage());
}
try
{
myList.getItemAt(0);
}
catch(EmptyListException ELE)
{
System.out.println(" getItemAt: " + ELE.getMessage());
}
System.out.println("End of EmptyListException test ");
}
// Method: printLengthAndList (LinkedList<Integer> list)
// Method to print both the length of the list and the
// contents of the list to the screen
public static void printLengthAndList(LinkedList<Integer> list)
{
System.out.printf("Length is: %d ", list.lengthIs());
list.print();
}
}
Explanation / Answer
ListNode Class
public class ListNode<T> {
private T data;
private ListNode<T> nextNode;
//Constructor 1
public ListNode(T data){
this.data = data;
this.nextNode = null;
}
// Constructor 2
public ListNode(T data, ListNode<T> nextNode){
this.data = data;
this.nextNode = nextNode;
}
//Implementation of functions said in UML
public void setData(T data){
this.data = data;
}
public T getData(T data){
return this.data;
}
public void setNext(ListNode<T> next){
this.nextNode = next;
}
}
LinkedList Class
public class LinkedList<T> {
ListNode<T> firstNode;
ListNode<T> lastNode;
int numElements;
String name;
public LinkedList(){
super();
}
public LinkedList(String name){
this.name = name;
}
/* Below is written taking following assumptions::
* if there is only one node, lastNode is set to null
*/
public void insertAtFront(T item){
ListNode node = new ListNode<T>(item);
if(firstNode!=null){
node.setNext(firstNode);
this.firstNode = node;
}else{
this.firstNode = node;
this.lastNode = null;
}
}
public void insertAtBack(T item){
ListNode node = new ListNode<T>(item);
if(lastNode!=null){
lastNode.setNext(node);
this.lastNode = node;
}else{
// Case when there is only one node
if(firstNode!=null){
firstNode.setNext(node);
this.lastNode = node;
}
// Case when there is no node
else{
this.firstNode = node;
this.lastNode = null;
}
}
}
}
// Other functions not implemented based on Chegg Guidelines of solving only certain no. of questions
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.