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.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 10PE
icon
Related questions
Question

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();

}

}

 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning