You have been provided with java code for SomeList class. This code is for a general linked list implementation where the elements are not ordered. For this assignment you will modify the code provided to create a SortedList class that will maintain elements in a linked list in ascending order and allow the removal of objects from both the front and back. You will be required to add methods for inserting an object in order (InsertInorder) and removing an object from the front or back. You will write a test program, ListTest, that inserts 25 random integers, between 0 and 100, into the linked list resulting in an in-order list. Your code to remove an object must include the exception NoSuchElementException. Demonstrate your code by displaying the ordered linked list and including the capability to remove nodes from the front and back.
JAVA CODE
Learning Objectives: Detailed understanding of the linked list and its implementation. Practice with inorder sorting. Practice with use of Java exceptions. Practice use of generics.
You have been provided with java code for SomeList<T> class. This code is for a general linked list implementation where the elements are not ordered. For this assignment you will modify the code provided to create a SortedList<T> class that will maintain elements in a linked list in ascending order and allow the removal of objects from both the front and back. You will be required to add methods for inserting an object in order (InsertInorder) and removing an object from the front or back. You will write a test program, ListTest, that inserts 25 random integers, between 0 and 100, into the linked list resulting in an in-order list. Your code to remove an object must include the exception NoSuchElementException. Demonstrate your code by displaying the ordered linked list and including the capability to remove nodes from the front and back.
//class to represent one node in a list
class ListNode<T> {
T data; // data for this node
ListNode<T> nextNode; // reference to the next node in the list
//constructor creates a ListNode that refers to object
ListNode(T object) {
this(object, null);
}
//constructor creates ListNode that refers to the specified
//object and to the next ListNode
ListNode(T object, ListNode<T> node) {
data = object;
nextNode = node;
}
//return reference to data in node
T getData() {
return data; // return item in this node
}
//return reference to next node in list
ListNode<T> getNext() {
return nextNode; // get next node
}
}
//class SomeList definition
public class SomeList<T> {
private ListNode<T> firstNode;
private ListNode<T> lastNode;
private String name; // string used in printing
//constructor creates empty List with "my list" as the name
public SomeList() {
this("my list");
}
//constructor creates an empty List with a name
public SomeList(String listName) {
name = listName;
firstNode = lastNode = null;
}
//insert item in front
public void insertAtFront(T insertItem) {
if (isEmpty()) {
firstNode = lastNode = new ListNode<T>(insertItem);
}
else {
firstNode = new ListNode<T>(insertItem, firstNode);
}
}
//determine whether list is empty
public boolean isEmpty() {
return firstNode == null; // return true if list is empty
}
//output list contents
public void print() {
if (isEmpty()) {
System.out.printf("Empty %s\n", name);
return;
}
System.out.printf("%s is: ", name);
ListNode<T> current = firstNode;
// while not at end of list, output current node's data
while (current != null) {
System.out.printf("%s ", current.data);
current = current.nextNode;
}
System.out.println();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps